作者yuchieh0 (小杰)
看板Statistics
标题[问题] 恳请解惑利用R软体完成回归、抽样等题组
时间Tue Apr 15 07:17:42 2008
请教一个需要用R完成的习题。原本题目是英文的,我根据题意先以中文说明一次:
首先以假设的截距、斜率,利用R随机产生符合标准常态分配的一组数字X,
共10,000笔资料,然後考虑误差项,以线型回归形式,跑出Y。此时若把Y跟
X跑简单回归,其截距跟斜率会跟先前所假设的差不多(此处截距为2、斜率为
5);若个别看X跟Y的叙述统计值,也对照前面的设定。从先前各10,000笔X与
Y的资料,反覆抽取1000笔数据、共100次,并跑100次简单回归式。最终把这
100个简单回归式的截距、斜率跟标准误放在一个矩阵里头,并利用R计算、
观察各参数的分配情形。
以下是我目前所拥有、写完的语法,已经卡在这里很久不知该怎麽办,其实自
己写得对错也没有头绪。然後同时间老师也急着催缴作业,所以就硬着头皮请
教大家。希望能寻得有人热心且帮得上忙。谢谢!
# 1. Gen erate a two-variable dataset satisfying the assumptions for
OLS (n=10,000)
x= rnorm(10000,0,1)
y= 2+5*x+rnorm(10000,0,1)
# 2. Run OLs on this population to determine your true parameters
lm=(formula= y~x)
summary(lm(y~x))
# 3. Grab a sample from the population and run OLS on it
x1= sample(x, 1000)
y1= sample(y, 1000)
lm=(formula= y1~x1)
summary(lm(y1~x1))
# 4. Keep track of your parameters (Betas, standard errors, and overall
sigma for the regression).
# 5. Do this sampling over and over again with new samples
RandomSamplex = matrix(1:1000, 100)
for(i in 1:100)
{
Rnumberx= sample(x, 1000)
RandomSamplex[,i]= as.matrix(Rnumberx)
}
RandomSampley = matrix(1:1000, 100)
for(i in 1:100)
{
Rnumbery= sample(y, 1000)
RandomSampley[,i]= as.matrix(Rnumbery)
}
# 6. Q1: What is the distribution of your betas?
# Q2: How does it compare to the theoretically predicted distribution?
# Q3: How do your estimated standard deviations compare to the predicted standard deviation?
# Q4: How does the estimate of sigma compare to the predicted sigma?
谢谢你(你)有耐心看完。
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 150.212.2.241
※ 编辑: yuchieh0 来自: 150.212.2.241 (04/15 07:18)
※ 编辑: yuchieh0 来自: 150.212.2.241 (04/15 07:19)
※ 编辑: yuchieh0 来自: 67.163.245.192 (04/15 13:35)
1F:→ bcs:library(boot) 写回圈满累的,fox的回归书有用boot的方法 04/15 23:49