Do the following question in R. Generate the full model with Ele] 0 and Varki-2,
ID: 3049474 • Letter: D
Question
Do the following question in R. Generate the full model with Ele] 0 and Varki-2, with a2-1. Moreover, set A,-1 and ,-2. You could use the following R code to generate the data. n = 200 x = rnorm (n) epsilon = rnorm(n) beta= 1 beta1 2 y = beta0+beta1*x+epsilon Use the 1m function in R to fit the model and record the regression coefficient, standard error, and residual sum of squares. Repeat this numerical studies for 100 times, and report the average value of the re- gression coefficients for , standard error for , and residual sum of squares. Explain whether the results you found coincide with what we have derived in class. Attach your R code when you submit your homework.Explanation / Answer
Run the code below in R:
set.seed(1001) #Setting seeds such that output remains same
m <- 100 #No. of simulations
beta <- numeric(m) # Beta_1 vector initialization
res <- numeric(m) #RSS initialization
for(i in 1:m){
n <- 200 # No. of samples
x <- rnorm(n)
epsilon <- rnorm(n)
beta0 <- 1
beta1 <- 2
y <- beta0+beta1*x+epsilon
fit <- lm(y~x) #Fitting linear regression
beta[i] <- as.numeric(fit$coefficients[2]) #Extracting beta_1
e <- fit$residuals
res[i] <- as.numeric(sum(e*e))
}
mean(beta)
sd(beta)
mean(res)
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.