将predict()的结果放在列表内的for循环中 [英] Place results of predict() in a for loop inside a list
问题描述
让我们说我想在不同的样本上多次在mtcars数据集上运行线性回归模型. 这个想法是,对于for循环中的每次迭代,每次运行线性回归时都要存储dictate()方法的结果 对于不同的样品.一个小例子如下:
Let us say I want to run the linear regression model on the mtcars dataset several times on different samples. The idea is, for each iteration in a for loop, to store the results of the predict() method every time the linear regression is run for a different sample. The small example follows for one run:
## 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()结果按行存储在数据框中.
I would like to perform this procedure several times inside a for loop, every time choosing a different sample and getting a correspondent Predictions() list, and store all the Predictions() results by line in a dataframe.
假设我为两个不同的样本运行模型.结果数据框的每一行都应该是该样本的上面的结果,例如:
Let's say I run the model for two different samples. Each row of the resulting dataframe should be the outcome above for that sample, like:
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.
关于如何执行此操作的任何想法?我已经开发出一些东西,但要么 抛出错误或仅存储最后的结果...我在这里错过了什么?
Any idea on how to go about doing this? I have developed something but it either throws an error or only stores the last result...What am I missing here?
这是我到目前为止所拥有的:
Here is what I have so far:
### 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
Error in
[[<-.data.frame
(*tmp*
, i, value = list(mpg = c(13.3, 10.4, : replacement has 10 rows, data has 0
推荐答案
我更喜欢使用magic_for()
,但是您也可以使用base R轻松地做到这一点.
I prefer to use magic_for()
however you can also do this with base R pretty easily.
这是一个例子:
Bootstrap_times <- 2
Sample_Size <- 10
Sample <- mtcars[sample(nrow(mtcars), Sample_Size), ]
Model <- lm(formula = mpg ~ wt, data = Sample)
Predictions <- predict(Model,newdata=mtcars)
## You like how I line up arrows, right?
Predictions <- t(Predictions)
Predictions <- list()
Results <-vector ("list",Bootstrap_times)## Stores the Predictions for each run
magicfor::magic_for()
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
put(predict(Model[[i]],newdata=mtcars))
}
tmp<-magicfor::magic_result_as_dataframe()
tmp
i predict(Model[[i]],newdata=mtcars)
1 1 22.858806
2 2 20.922763
3 1 25.136504
4 2 18.341372
5 1 16.633098
6 2 16.481252
7 1 15.646096
8 2 18.531180
9 1 18.834873
10 2 16.633098
11 1 16.633098
12 2 11.849933
13 1 14.431324
14 2 14.051708
15 1 2.890988
16 2 1.569924
17 1 2.169717
18 2 26.047583
19 1 30.489093
20 2 28.818782
21 1 24.035616
22 2 16.025712
23 1 16.671060
24 2 13.596168
25 1 13.558206
26 2 28.059549
27 1 26.503122
28 2 31.263511
29 1 18.683026
30 2 21.719957
31 1 15.646096
32 2 21.644034
33 1 22.978374
34 2 21.584264
35 1 24.618503
36 2 19.725450
37 1 18.495353
38 2 18.386011
39 1 17.784630
40 2 19.862128
41 1 20.080812
42 2 18.495353
43 1 18.495353
44 2 15.051081
45 1 16.909894
46 2 16.636540
47 1 8.599905
48 2 7.648629
49 1 8.080530
50 2 25.274555
51 1 28.472808
52 2 27.270046
53 1 23.825774
54 2 18.057985
55 1 18.522689
56 2 16.308514
57 1 16.281178
58 2 26.723336
59 1 25.602581
60 2 29.030452
61 1 19.971470
62 2 22.158309
63 1 17.784630
64 2 22.103638
这篇关于将predict()的结果放在列表内的for循环中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!