【发布时间】:2018-12-27 23:29:13
【问题描述】:
假设我想在不同的样本上多次运行 mtcars 数据集上的线性回归模型。 这个想法是,对于 for 循环中的每次迭代,每次运行线性回归时都存储 predict() 方法的结果 对于不同的样本。运行一次的小例子如下:
## Perform model once on a Sample and use model on full dataset:
Sample_Size <- 10
Sample <- mtcars[sample(nrow(mtcars), Sample_Size), ]
Model <- lm(formula = mpg ~ wt, data = Sample)
Predictions <- predict(Model,newdata=mtcars)
## Gets us a list with predicted wt for each car:
Predictions <- t(Predictions)
这会产生
> Predictions
Mazda RX4 Mazda RX4 Wag Datsun 710 Hornet 4 Drive Hornet Sportabout
[1,] 25.80494 23.89161 28.05592 21.34051 19.65228
Valiant Duster 360 Merc 240D Merc 230 Merc 280 Merc 280C Merc 450SE
[1,] 19.50221 18.67685 21.52809 21.82822 19.65228 19.65228 14.92523
Merc 450SL Merc 450SLC Cadillac Fleetwood Lincoln Continental
[1,] 17.47633 17.10117 6.071394 4.765828
.... and so on for other cars
我想在 for 循环中多次执行此过程,每次 选择不同的样本并获得对应的 Predictions() 列表, 并将所有 Predictions() 结果逐行存储在数据框中。
假设我为两个不同的样本运行模型。结果数据帧的每一行都应该是该样本的上述结果,例如:
Mazda RX4 Mazda RX4 Wag Datsun 710 Hornet 4 Drive Hornet Sportabout
[1,] 25.80494 23.89161 28.05592 21.34051 19.65228
[2,] 22.80492 22.89147 28.05532 21.34231 20.65290
Valiant Duster 360 Merc 240D Merc 230 Merc 280 Merc 280C Merc 450SE
[1,] 19.50221 18.67685 21.52809 21.82822 19.65228 19.65228 14.92523
[2,] 21.83492 23.84147 29.02532 21.34231 20.35290 18.45228 13.92523
... and so on for other cars.
关于如何进行此操作的任何想法?我已经开发了一些东西,但它要么 引发错误或仅存储最后一个结果...我在这里缺少什么?
这是我目前所拥有的:
### Inside a for loop, to get a dataframe of Predictions:
Bootstrap_times <- 2
Sample_Size <- 10
Predictions <- list()
Results <-vector ("list",Bootstrap_times)## Stores the Predictions for each run
for(i in 1:Bootstrap_times){
### Take a sample
Sample[[i]] <- mtcars[sample(nrow(mtcars), Sample_Size), ]
### Do the regression on the sample
Model[[i]] <- lm(formula = mpg ~ wt, data = Sample[[i]])
### Perform the predict() on the sample
Predictions[[i]] <- predict(Model[[i]],newdata=mtcars)
### put the result as a line on the dataframe Results
Predictions[[i]] <- t(Predictions[[i]])
return(Predictions)
}
但是,我不断收到:
[[<-.data.frame(*tmp*, i, value = list(mpg = c(13.3, 10.4, : 替换有10行,数据有0
【问题讨论】:
-
谢谢,我意识到缺少一些东西,即 Sample_Size 参数。现在将添加它,干杯
标签: r for-loop linear-regression