Manually bootstrapping linear regression in R

十年热恋 提交于 2021-02-10 11:49:11

问题


|Hi guys, I am asking you for help as I am stucked with bootstrapping...

The task is: Use the nonparametric bootstrap to compute bootstrap standard error of CAPM beta estimate based on 1000 bootstrap replications and bootstrap sample size equal to the size of the original sample.

If I understand it correctly, I am supposed to run my regression model 1000 times to estimate different estimates of the beta and its standard error. However, I am not able to put my thoughts into an actual R code.

My code:

#1)fetch data from Yahoo
#AAPL prices
apple08 <- getSymbols('AAPL', auto.assign = FALSE, from = '2008-1-1', to = 
"2008-12-31")[,6]
#market proxy
rm08<-getSymbols('^ixic', auto.assign = FALSE, from = '2008-1-1', to = 
"2008-12-31")[,6]

#log returns of AAPL and market
logapple08<- na.omit(ROC(apple08)*100)
logrm08<-na.omit(ROC(rm08)*100)

#OLS for beta estimation
beta_AAPL_08<-summary(lm(logapple08~logrm08))$coefficients[2,1]

OK, I've got the coefficient estimate of AAPL beta for '08. Now, I would like to run bootstrap on the beta and its standard error 1000 times with the sample size same as the original.

set.seed(666)
Boot_times=1000
mean.boot=rep(0,Boot_times)
for(i in 1:Boot_times){
# nonparametric bootstrap
data.boot=#Here I am stucked, I dunno what to put here
boot[i]=data.boot
}

I have thought about using

summary(lm(sample(logapple08, size=length(logapple08), replace = 
TRUE)~sample(logrm08, size=length(logrm08), replace = 
TRUE)))$coefficients[2,1]

However I guess it is not correct. I do resample the returns, however I assume that it resamples the data withouth taking care of the dates of the returns, i.e. it regresses for instance AAPL return from 25/1/08 to a market's return which happened on 25/2/08.

Help would be appreciated, thank you!

Adam


回答1:


In order to bootstrap a linear regrassion computed with lm you can do something following the lines of the code below.

library(boot)

# This is the function 'statistic'    
boot_lm_coef <- function(data, index){
    coef(lm(logapple08 ~ logrm08, data = data[index, ]))[2]
}

df_boot <- data.frame(logapple08, logrm08)

set.seed(666)
Boot_times <- 1000
result <- boot(df_boot, boot_lm_coef, R = Boot_times)
mean(result$t)
#[1] 1.078191

Notes:

  1. I have used coef, not summary(lm(.))[2, 1] which is overkill
  2. The data must be a data.frame for lm to work.


来源:https://stackoverflow.com/questions/47576603/manually-bootstrapping-linear-regression-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!