Question

Problem 2 (0.5 x 3 = 1.5 point ) Simulate a sample of y1, ..., 4100 from a simple linear model Y = 1 + 2x + €, where € ~ N(0, Please include all code from R that was used to calculate everything.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

set.seed(1)
library(ggplot2)
epsilon=rnorm(100,0,6)
x=c(1:100)
y=1+2*x + epsilon
data=data.frame(x,y)

fit1=lm(y~x,data=data)
fit1
fit1$coefficients
ggplot(data , aes(x = x, y = y)) + geom_point(size = 0.8) + stat_smooth(method = 'lm', formula = y ~ x)
summary(fit1)

# t statistic to check b1=2
#from summary we found, standard error of b1 = 0.02001
se=0.02001
b1=2
n=100
t=(as.numeric(fit1$coefficients[2])-2)/se
p_value =1-pt(abs(t),n-1)
# value comes out to be 0.403835 => We fail to reject null hypothesis that b1=2 and conclude that b1=2

#-------------------------
b00=c()
b11=c()
for(i in 1:100){
set.seed(i)
x1=c(1:100)
e=rnorm(100,0,6)
y1=1+2*x1 + e
data1=data.frame(x1,y1)
fit2=lm(y1~x1,data=data1)
b00=c(b00,as.numeric(fit2$coefficients[1]))
b11=c(b11,as.numeric(fit2$coefficients[2]))
}
cor(b00,b11)
#correlation comes out to be -0.892052

200 150 >100- 50- 0- 100 50 75 X -25

Add a comment
Know the answer?
Add Answer to:
Please include all code from R that was used to calculate everything. Problem 2 (0.5 x...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT