1439 2

[问答] R软件关于正态分布的线性关系题目求教兼资料分享 [推广有奖]

  • 0关注
  • 2粉丝

硕士生

9%

还不是VIP/贵宾

-

威望
0
论坛币
1902 个
通用积分
1.1950
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
3419 点
帖子
54
精华
0
在线时间
101 小时
注册时间
2016-1-19
最后登录
2019-12-7

15论坛币
1. Simulate a data set with 40 observations: X ~ N(u = 3; v = 4), Y = 2 - 6X + e where
e ~ N(u = 0; v= 4). Use this data set to do a regression analysis
(a) Draw a scatter plot
(b) Write your own R code to calculate 0 and 1. (Don't use lm())
(c) Calculate con dence interval for 0 and 1.(Don't use lm())
(d) Suppose there is a new X = 2:7, calculate the con dence interval for E(Y jX = 2:7) and the
prediction interval. ((Don't use lm()))
(e) Use lm() to verify your answer in part (b)-(d).
如何将如下思路转换成代码?求代码b-d

2006 薛毅 统计建模与R软件.zip (3.82 MB)




capture image.png (267.25 KB)

capture image.png

关键词:资料分享 正态分布 线性关系 r软件 性关系 软件
第三题还是有问题...
# Input
X = rnorm(40,-0.5,1)
e = rnorm(40,0,4 )
Y=  2-6*X+e

#a. Draw a scatter plot
plot(X,Y)

#b. Write your own R code to calculate Beta0 and Beta1
x=mean(X)
y=mean(Y)
i=1
Sxy=0
Sxx=0
for(i in 1:40)
{
   
   a=X[i]
   b=Y[i]
   Sxy=(a-x)*(b-y)+Sxy
   Sxx=(a-x)^2+Sxx
}
Beta1=Sxy/Sxx
Beta0=y-Beta1*x

#c. Calculate confidence interval for Beta0 and Beta1
j=1
Betaint1=0
Betaint0=0
for(j in 1:40)
{
   X = rnorm(40,-0.5,1)
   e = rnorm(40,0,4 )
   Y=  2-6*X+e
   Sxy=0
   Sxx=0
   for(k in 1:40)
   {
     x=mean(X)
     y=mean(Y)
     a=X[k]
     b=Y[k]
     Sxy=(a-x)*(b-y)+Sxy
     Sxx=(a-x)^2+Sxx
   }
   Beta1=Sxy/Sxx
   Beta0=y-Beta1*x
Betaint1[j]=Beta1
Betaint0[j]=Beta0
}
t.test(Betaint1,df=38)
?t.test
#d. Suppose there is a new X = 2:7, calculate the confidence interval for E(Y jX = 2:7) and the prediction interval. ((Don't use lm()))
i=1
Predictint=0
for(i in 1:40)
{
   e=rnorm(40,0,2)
   Y=2-6*2.7+e
   y=mean(Y)
   Predictint[i]=y
}
t.test(Predictint)

#e. Use lm() to verify your answer in part (b)-(d).
lm2=lm(X~Y)
summary(lm2)
con=confint(lm2)
con
predict(lm2, interval="predict")
predict(lm2, interval="confidence")

使用道具

藤椅
clb_polaris 发表于 2017-8-14 20:23:21 |只看作者 |坛友微信交流群
太感人了

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-25 17:55