在R中手动引导线性回归 [英] Manually bootstrapping linear regression in R

查看:45
本文介绍了在R中手动引导线性回归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

|大家好,我在寻求引导时遇到了麻烦...

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

任务是:使用非参数引导程序,根据1000次引导程序复制和相等于原始样本大小的引导程序样本大小,计算CAPM beta估计的引导程序标准误差.

如果我正确理解它,我应该将回归模型运行1000次,以估计beta及其标准误的不同估计.但是,我无法将想法转化为实际的R代码.

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.

我的代码:

#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]

好的,我已经得到了 08 年 AAPL 测试版的系数估计值.现在,我想在Beta及其标准错误上运行bootstrap 1000次,并且样本大小与原始样本相同.

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
}

我已经考虑过使用

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

但是我猜这是不正确的.我确实对收益进行了重采样,但是我假设它对数据进行了重采样而没有照顾到收益的日期,即它将例如APL收益从25/1/08回归到市场收益,该收益发生在25/2/08.

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!

亚当

推荐答案

为了引导使用 lm 计算的线性折旧,您可以按照以下代码行进行操作.

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

注意:

  1. 我用过 coef ,而不是 summary(lm(.))[2,1] ,这太过分了
  2. 数据必须是 data.frame 才能使 lm 起作用.
  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.

这篇关于在R中手动引导线性回归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆