我正在處理一項任務。我已經給了一些誠實的嘗試,所以我想我會伸出手來尋求幫助。
我必須以 10 個樣本大小運行引導程式 10 次,并撰寫一個函式來估計每組輸入的線性回歸模型。這不是完整的問題,但是,這是我堅持的部分。如果你想要完整的問題,請告訴我。
這是我迄今為止嘗試過的代碼。x 和 y 資料應被視為對(x_i, y_i):
rm(list=ls())
x = c(1,1.5,2,3,4,4.5,5,5.5,6,6.5,7,8,9,10,11,12,13,14,15)
y = c(6.3,11.1,20,24,26.1,30,33.8,34.0,38.1,39.9,42,46.1,53.1,52,52.5,48,42.8,27.8,21.9)
n=length(y)
myfunc <- function(data,index){
# Calculate and return the estimate of linear regression model
lmout <- lm(data)
return(lmout$estimate)
}
# call boot
library(boot)
bout = NULL
# Calling boot 10 times...
for(i in 1:10){
#... with a bootstrap distribution of size 10
bout = boot(data = y ~ x, statistic = myfunc, R = 10)
}
print(bout$t)
我的問題是,當 I 時print(bout$t),它顯示一個沒有值的列:
[1,]
[2,]
[3,]
[4,]
[5,]
[6,]
[7,]
[8,]
[9,]
[10,]
在myfunc( print(lmout)) 中添加列印陳述句會回傳以下輸出 100 次:
Coefficients:
(Intercept) x
21.321 1.771
我的假設是我生成引導程式輸入的方式出現問題,或者我回傳它時出現問題。
uj5u.com熱心網友回復:
這似乎更像是一個合理的答案:
library(boot)
data <- data.frame(
x = c(1,1.5,2,3,4,4.5,5,5.5,6,6.5,7,8,9,10,11,12,13,14,15),
y = c(6.3,11.1,20,24,26.1,30,33.8,34.0,38.1,39.9,42,46.1,53.1,52,52.5,48,42.8,27.8,21.9))
myfunc <- function(data, index){
# Calculate and return the estimate of linear regression model
lmout <- lm(y ~ x, data = data[index,])
coef(lmout)
}
myfunc(data.frame(x,y)) # always run this once to see if you function makes sense
boot(data = data.frame(x,y), statistic = myfunc,
R = 250)
該R =顯示應該有多少次發生的引導。該index
引數是什么決定了新的“自舉”采樣。資料必須在 data.frame 中,否則它不會放在 -packageboot可以抓住它的地方。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/354168.html
下一篇:檢索頂部和底部值R資料框
