Using the simulation data of the given R code, find the LSE value and report the
ID: 3371670 • Letter: U
Question
Using the simulation data of the given R code, find the LSE value and report the estimated coefficient value and R2.
At this time, the seed number should be the attendance number of each person, and interpret the estimated LSE value and R2 value.
#Simulation example
set.seed(0)
n=1000;
x1=rnorm(n)
#x2=runif(n)
> u=rnorm(n)
y=one+x1+u
#y=one+x1+x2+u;
x=cbind(one,x1)
#x=cbind(one,x1,x2)
k=ncol(x)
invx=solve(t(x)%*%x)
xy=t(x)%*%y
bhat=invx%*%xy
bhat
model1=lm(y~x1)
model1$coefficients
plot(x1,model1$fitted.values)
points(x1,y)
summary(model1)
yhat=x%*%bhat
cbind(yhat,model1$fitted.values)
r=y-yhat
r2=r^2
rsq=1-sum(r2)/sum((y-mean(y))^2)
#Real data Example
cars
simreg=lm(dist~speed, data=cars)
print(simreg)
Explanation / Answer
## Simulated data
> summary(model1)
Call:
lm(formula = y ~ x1)
Residuals:
Min 1Q Median 3Q Max
-3.0180 -0.7024 -0.0008 0.7504 3.0451
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.97501 0.03271 29.81 <2e-16 ***
x1 0.98693 0.03279 30.10 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 1.034 on 998 degrees of freedom
Multiple R-squared: 0.4759, Adjusted R-squared: 0.4754
F-statistic: 906.1 on 1 and 998 DF, p-value: < 2.2e-16
Comment: From the above table estimated coefficient values of intercept and slope are ?0.97501 and ? 0.98693 respectively with R^2 value 0.4759.
## For the Car data
> summary(simreg)
Call:
lm(formula = dist ~ speed, data = cars)
Residuals:
Min 1Q Median 3Q Max
-29.069 -9.525 -2.272 9.215 43.201
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -17.5791 6.7584 -2.601 0.0123 *
speed 3.9324 0.4155 9.464 1.49e-12 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 15.38 on 48 degrees of freedom
Multiple R-squared: 0.6511, Adjusted R-squared: 0.6438
F-statistic: 89.57 on 1 and 48 DF, p-value: 1.49e-12
Comment: From the above table estimated coefficient values of intercept and slope are ?-17.5791 and 0.9324 respectively with R^2 value 0.6511.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.