PLEASE ANSWER THIS QUESTION IN R!!! 3. This problem is an extension of Question
ID: 3074453 • Letter: P
Question
PLEASE ANSWER THIS QUESTION IN R!!!
3. This problem is an extension of Question 4 from Homework 2, where you generated realizations from a simple linear model. Here you will be repeatedly simulating data and constructing confidence intervals for A (part b), confidence intervals for the mean response (part c), and prediction intervals for a new observation (part d). The linReg.simulation_hw5.R file posted on Canvas provides template pieces for accomplishing this a. Generate a realization of one dataset with n-20 observations from a simple linear model rmal distribution with mean 0 where Ei has a normal distribution with miean 0 and standard deviation 2. Let ß0 = 3 and A = 1. Let xi = 1,T2 = 2, ,x20 = 20. Estimate and compute a 95% confidence interval for A; report the point estimate and confidence interval. (You may use the confintO function.) b. Repeat part (a) 100 times, generating new data (and hence a new confidence interval) for each Repeat uut a iteration. Report the percentage of CI's which contain the true value 1. (You do not need to report all 100 estimates or intervals.) Report the true value of the mean of Y when xo -2.5. Generate n-20 observations as in (a), fit a linear model to the generated data, and compute the 95% confidence interval for the mean response when - 2.5. Report this confidence interval. Repeat this procedure 100 times. Report the percentage of CI's that contain the true mean response. (You do not need to report all 100 intervals.) d. Again, generate a dataset of n 20 observations as in (a) and fit a linear model. Compute the point prediction and a 95% prediction interval for a new observation with 20-25. Report this prediction and the prediction interval. Now, generate 100 e's, and plug these into the model to generate 100 new observations with x = 2.5. Report the percentage of these new observations are within the prediction interval you cread. (You do not need to report all 100 observations e. Provide the code used for parts b, c, and dExplanation / Answer
## Problem (a)
e=rnorm(20,0,2)
b0=3 ; b1=1
x=1:20
y=b0+b1*x+e # generating observation
model1=lm(y~x)
b1_est= coefficients(model1)[2] # estimated b1
summary(model1) # summary of the model
c=confint(model1,level = 0.95)[2,] # Confidence interval for b1
###################################
## problem (b)
c=list()
for(i in 1:100)
{
e=rnorm(20,0,2)
b0=3 ; b1=1
x=1:20
y=b0+b1*x+e
c[[i]]=unname(confint(lm(y~x),level = 0.95)[2,])
}
logical=NULL
for(i in 1:100 )
{
logical[i]=c[[i]][1]<b1&c[[i]][2]>b1
}
per=table(logical)
Percentage=per[[2]] # percentage of values lie in the confidenceinterval
##############################
## problem (c)
newdta=data.frame(x=2.5)
p=predict(model1,newdata = newdta, interval = 'confidence') ## fitted value with confidence interval
x_pred=p[1]
c=list()
for(i in 1:100)
{
e=rnorm(20,0,2)
b0=3 ; b1=1
x=1:20
y=b0+b1*x+e
model1=lm(y~x)
newdta=data.frame(x=2.5)
p=predict(model1,newdata = newdta, interval = 'confidence') ## fitted value with confidence interval
c[[i]]=unname(c(p[2],p[3]))
}
logical=NULL
for(i in 1:100 )
{
logical[i]=c[[i]][1]<x_pred&c[[i]][2]>x_pred
}
per=table(logical)
Percentage=per[[2]] # percentage of values lie in the confidenceinterval
#########################################
##problem (d)
newdta=data.frame(x=2.5)
p=predict(model1,newdata = newdta, interval = 'confidence') ## fitted value with confidence interval
x_pred=p[1]
c=list()
for(i in 1:100)
{
e=rnorm(100,0,2) ## 100 random sample
b0=3 ; b1=1
x=1:100 ## 100 observation
y=b0+b1*x+e
model1=lm(y~x)
newdta=data.frame(x=2.5)
p=predict(model1,newdata = newdta, interval = 'confidence') ## fitted value with confidence interval
c[[i]]=unname(c(p[2],p[3]))
}
logical=NULL
for(i in 1:100 )
{
logical[i]=c[[i]][1]<x_pred&c[[i]][2]>x_pred
}
per=table(logical)
Percentage=per[[2]] # percentage of values lie in the confidenceinterval
[ All the codes are verified in my machine(windows 10, R-studio 1.0.153) If you have any doubts regarding any line feel free to ask me ]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.