Question

Use R to tackle the problem Write a function which takes as input x = (x1,...

Use R to tackle the problem

Write a function which takes as input x = (x1, . . . , xn) and y = (y1, . . . , yn) and outputs the slope and intercept given by the method of least squares.

Homework Answers

Answer #1
 
The first thing to do is to specify the data. Here x is the explanatory variable and y is the response variable.
> x <- c(x1,x2,....,xn)
> y <- c(y1,y2,....,yn)
Input the values instead of x1,x2,....,xn and y1,y2,....,yn
Next is to check for the linearity of the data by constructing a scatter plot and measuring the correlation coefficient.The output will be correlation coefficient. Least square regression can be performed only if there is linearity for that the value of correlation coefficient must be high in magnitude ie between -1 and -0.7 or 0.7 and 1
> plot(x,y)
> cor(x,y)
Next is to fit a linear regression with the method of least squares. The command used here is lm
Use help(lm) command to learn more. 
> fit <- lm(rate ~ year)
> fit
lm command will give you two things the slope and the intercept. eg The figure below x is the slope.
Coefficients:
(Intercept)         x
   420.208       0.5301

Feel free to contact incase of doubts, thankyou

 
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
In R- Studio : Write a function that takes as an input a positive integer and...
In R- Studio : Write a function that takes as an input a positive integer and uses the print() function to print out all the numbers less than the input integer. (Example: for input 5, the function should print the numbers 1,2,3,4 { for input 1, the function should not print a number.) Write a recursive function, do not use any of the loop commands in your code.
N particles can move in plane (x,y). Write down coordinates and momenta of all particles forming...
N particles can move in plane (x,y). Write down coordinates and momenta of all particles forming the phase space and determine number of degrees of freedom s. (a) x1, px1, y1, py1, x2, px2, y2, py2,…, xN, pxN, yN, pyN , s=2N (b) x1, px1, x2, px2, …, xN, pxN , s=2N (c) y1, py1, y2, py2,…, yN, pyN , s=2N (d) x1, y1, x2, y2, …, xN, yN , s=2N and why you choose
Complete the following question in R. Problem 5. Write a function in R called like which...
Complete the following question in R. Problem 5. Write a function in R called like which takes an input vector dat, assumed to be Poisson random variates, and computes the Maximum-Likelihood Estimator for λ
Write a function find square root(x) that takes as input a number x and as output...
Write a function find square root(x) that takes as input a number x and as output returns the square root of x. Your results should converge to at least 6 decimal places. matlab question
Write an R function that takes three input values which would be three coefficients, solves quadratic...
Write an R function that takes three input values which would be three coefficients, solves quadratic equations, and prints both roots. If the roots are the same, print the root only once. # If there are no real roots, print the message "There are no real roots." For information of the quadratic equations, here is the Wiki page link https://en.wikipedia.org/wiki/Quadratic_equation Your code below Then call the function you've just created and input the coefficients for the equations 3x^2 +5x +...
Use R to tackle the following problem Suppose there is a class of 23 people with...
Use R to tackle the following problem Suppose there is a class of 23 people with birthdays given by the random variables X1, . . . , X23. Suppose that the random variables are independent and that each Xi has the property that P(Xi = k) = 1 365 for 1 ≤ k ≤ 365; thus we make the assumption that there are no leap years and that people are equally likely to be born on each day of the...
Write a function in Matlab that takes as input the number of iterations k, the size...
Write a function in Matlab that takes as input the number of iterations k, the size n, and a sparse matrix A. Have this function run the Power method for k iterations on an initial guess the vector of 1’s and output the dominant eigenvalue and its corresponding eigenvector. Use only basic programming. Write out or print out your function.
3. Write function, leastChar(inputString) that takes as input a string of one or more letters (and...
3. Write function, leastChar(inputString) that takes as input a string of one or more letters (and no other characters) and prints 1) the "least" character in the string, where one character is less than another if it occurs earlier in the alphabet (thus 'a' is less than 'C' and both are less than 'y') and 2) the index of the first occurrence of that character. When comparing letters in this problem, ignore case - i.e. 'e' and 'E' should be...
Write a function called TaylorSin.m that takes as input an array x, and positive integer N,...
Write a function called TaylorSin.m that takes as input an array x, and positive integer N, and returns the Nth Taylor polynomial approximation of sin(x), centered at a = 0. The first line of your code should read function s = TaylorSin(x,N) HINT: in computing k!, use kfact = k*(k-1)*kfact since you are counting by 2
TO BE DONE IN PYTHON: Write a function `lowest_integer()` which takes 2 input arguments: 1)a function...
TO BE DONE IN PYTHON: Write a function `lowest_integer()` which takes 2 input arguments: 1)a function `g` representing an increasing function g(x) 2) a number `gmin`, and returns an integer `nmin` such that nmin>0 is the smallest integer that satisfies g(nmin)>gmin. test: def g(n): return 2*n print(lowest_integer(g, 10)) Output: 6
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT