作者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