Let X ∼ N(0, 1), ∼ N(0, 1). Let Y = 1.75 + 2X + . Generate 100 samples for (X, Y ). Use the generated data to fit a linear regression. (a) Report the fitted coefficients and intercept. (b) Draw a scatter plot of (X, Y ). Add the fitted line and the real line to the scatter plot with different color.
Using R
All R commands are shown in bold.
Generate 100 samples for (X, Y ).
x = rnorm(100)
e = rnorm(100)
y = 1.75 + 2 * x + e
Use the generated data to fit a linear regression.
model = lm(y~x)
model
Call:
lm(formula = y ~ x)
Coefficients:
(Intercept) x
1.763705 1.997952
(a)
fitted coefficients = 1.997952
intercept = 1.763705
(b)
Scatter plot of (X, Y ) is,
plot(x, y, pch = 16, col = "blue")
Add the fitted line and the real line to the scatter plot with different color.
abline(model, col = "red") #Fitted line
lines(x, 1.75 + 2 *x, col = "green") # Real line
Get Answers For Free
Most questions answered within 1 hours.