【发布时间】:2012-08-29 15:53:41
【问题描述】:
我有一个函数定义为:
foo<-function(data){
for (i in 2:10)
run.model<-mark(data[sample(nrow(data), i),], model="Occupancy")
results<-data.frame(mean(summary(run.model)$real$p), summary(run.model)$real$Psi, i)
return(results)
}
“mark”是运行我感兴趣的模型的函数。但是,结果仅包含 i=10
的最后一个模型 mean.summary.run.model..real.p. X1 i
1 0.1403083 0.6414447 10
如何更正我的函数,以便将结果从 i=2 编译为 i=10?
(无法回答我自己的问题,因此我编辑了我的问题以显示我如何修改您的代码:
谢谢你们。
我修改了@David Robinson 的代码
foo<-function(data){
do.call(rbind, lapply(2:6, function(i){
run.model<-mark(data[sample(nrow(data), i),], model="Occupancy")
cbind(p=mean(summary(run.model)$real$p), Psi=summary(run.model)$real$Psi, stations=i)
}))
}
得到了这些输出:
p 1 stations
0.4895234 1.388066e-10 2
0.2902716 3.445050e-01 3
0.0942734 7.955582e-01 4
0.1683427 2.376106e-01 5
0.1683427 1.980088e-01 6
我想知道为什么我命名了第二列但它没有出现在输出中?
对于@zzk 的代码,我将它们修改如下:
foo<-function(data){
results.frame <- data.frame()
for (i in 2:6) {
run.model<-mark(data[sample(nrow(data), i),], model="Occupancy")
results<-data.frame(p=mean(summary(run.model)$real$p), Psi=summary(run.model)$real$Psi, stations=i)
results.frame <- rbind(results.frame, results)
}
return(results.frame)
}
还有输出:
p X1 stations
1 0.1683427 5.940264e-01 2
2 0.5533567 7.292506e-12 3
3 0.0500000 1.000000e+00 4
4 0.1683427 7.128317e-01 5
5 0.2321999 3.588861e-01 6
差不多。
其他问题是: 1.如果我想重复这个循环n次,我想使用函数“replicate”。但我不知道该怎么说。 2. 是否可以将输出设置为 data.frame 以便我稍后对其进行操作? (例如计算均值、制作图表、分组...等)
我用过 复制(10,foo(数据))
这是我得到的。看起来输出变得有问题并且行和列被反转了。与 "replicate(100, foo(data), simple="data.frame")" 的结果相同。
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
p Numeric,3 Numeric,3 Numeric,3 Numeric,3 Numeric,3 Numeric,3 Numeric,3 Numeric,3 Numeric,3 Numeric,3
X1 Numeric,3 Numeric,3 Numeric,3 Numeric,3 Numeric,3 Numeric,3 Numeric,3 Numeric,3 Numeric,3 Numeric,3
se.p Numeric,3 Numeric,3 Numeric,3 Numeric,3 Numeric,3 Numeric,3 Numeric,3 Numeric,3 Numeric,3 Numeric,3
se.Psi Numeric,3 Numeric,3 Numeric,3 Numeric,3 Numeric,3 Numeric,3 Numeric,3 Numeric,3 Numeric,3 Numeric,3
stations Integer,3 Integer,3 Integer,3 Integer,3 Integer,3 Integer,3 Integer,3 Integer,3 Integer,3 Integer,3
但如果我使用此代码(输出中还有 1 列)
foo<-function(data){
do.call(rbind, lapply(2:4, function(i){
run.model<-mark(data[sample(nrow(data), i),], model="Occupancy")
cbind(mean(summary(run.model)$real$p), Psi=summary(run.model)$real$Psi, se.p=mean(summary(run.model, se=T)$real$p$se), stations=i)
}))
}
与
replicate(5, foo(data))
我明白了
, , 1
1 se.p stations
0.4895234 1.388066e-10 0.0000000 2
0.0333333 1.000000e+00 0.0327731 3
0.2117159 8.265795e-01 0.0833965 4
, , 2
.....
.....
, , 5
1 se.p stations
0.2902716 0.5167575 0.1519857 2
0.2000000 1.0000000 0.0730297 3
0.2902716 0.2583787 0.1519857 4
有 复制(5,foo(数据),simplify="data.frame")
我得到了这些。
[,1] [,2] [,3] [,4] [,5]
[1,] 4.895234e-01 1.683427e-01 4.895234e-01 1.683427e-01 0.1683427
[2,] 1.683427e-01 5.533567e-01 2.902716e-01 5.533567e-01 0.0666667
[3,] 2.500000e-02 2.117159e-01 2.321999e-01 3.974777e-01 0.0250000
[4,] 1.388066e-10 5.940264e-01 1.388066e-10 5.940264e-01 0.5940264
[5,] 3.960176e-01 7.292506e-12 3.445050e-01 7.292506e-12 1.0000000
[6,] 1.000000e+00 8.265795e-01 5.383291e-01 2.515864e-01 1.0000000
[7,] 0.000000e+00 1.379382e-01 0.000000e+00 1.379382e-01 0.1379382
[8,] 1.379382e-01 0.000000e+00 1.519857e-01 0.000000e+00 0.0455420
[9,] 2.468550e-02 8.339650e-02 1.038181e-01 1.575997e-01 0.0246855
[10,] 2.000000e+00 2.000000e+00 2.000000e+00 2.000000e+00 2.0000000
[11,] 3.000000e+00 3.000000e+00 3.000000e+00 3.000000e+00 3.0000000
[12,] 4.000000e+00 4.000000e+00 4.000000e+00 4.000000e+00 4.0000000
我需要的是,如果我每次重复 3 次:
p X1 stations
1 0.1683427 5.940264e-01 2
2 0.4687956 0.9876516334 2
3 xxxxxxxx xxxxxxxxxxxx 2
4 xxxxxxxxx xxxxxxxxxxxx 3
5 0.5533567 7.292506e-12 3
6 xxxxxxxxx xxxxxxxxxxxx 3
.................................
13 0.0500000 1.000000e+00 6
14 0.1683427 7.128317e-01 6
15 0.2321999 3.588861e-01 6
【问题讨论】:
-
您希望复制的结果是什么格式?也就是说,您是否想获得相同的输出,但使其更宽?如果是这样,为 p 和 psi 创建单独的矩阵可能会更容易,其中每一列都是单独的复制,不是吗?
-
其实你可以回答你自己的问题。 (可能会有时间限制,如果大卫罗宾逊的回答基本正确,那么这样做会很糟糕。如果不是,那么你应该看看是否已经过了必要的时间。)
-
相信我回答了你的“复制”问题,在下面编辑了我的答案。
-
谢谢大家。我实际上想每次复制 n 次并将结果附加到数据框中。也许这应该通过添加另一个 for 循环而不是复制来实现?
-
我知道我可以回答我自己的问题,但我不得不等待 8 小时。不能等那么久..:P