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.
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
Get Answers For Free
Most questions answered within 1 hours.