一起使用 by 和 mapply

using by and mapply together

您好,我正在尝试以一组因子水平为条件获取 ICC 值。例如:

usr1<-data.frame(a1=1:5,a2=11:15,a3=21:25,bin=factor(c("a","b","a","a","b")))
usr2<-data.frame(a1=2:6,a2=12:16,a3=32:36)

我想要a1、a2、a3的ICC,其中bin是a和b。在对这个因素进行调节之前,我使用了 mapply

mapply(function(x,y){z<-data.frame(x,y);icc(z,model="t",type="a")},usr1[,-4],usr2)

我不知道如何用 by 包装这个来为每个因素水平做这个。

mapply(function(x,y){z<-data.frame(x,y);icc(z,model="t",type="a")},usr1[c(1,3,4),-4],usr2[c(1,3,4),])

应该给你 "a" 级别的正确答案 和

mapply(function(x,y){z<-data.frame(x,y);icc(z,model="t",type="a")},usr1[c(2,5),-4],usr2[c(2,5),])

应该会给你 "b" 级别的正确答案。

这是我的问题的一个非常简化的版本,我必须为 20 个级别计算 200 多个 ICC。我事先不知道哪些行有哪些因素,除了我在 data.frame.
中有它 谢谢

编辑

预期的输出就像....

usr1$bin:a
           a1          a2          a3          
subjects   3           3           3           
raters     2           2           2           
model      "twoway"    "twoway"    "twoway"    
type       "agreement" "agreement" "agreement" 
unit       "single"    "single"    "single"    
icc.name   "ICC(A,1)"  "ICC(A,1)"  "ICC(A,1)"  
value      0.8235294   0.8235294   0.03713528  
r0         0           0           0           
Fvalue     -5.2542e+15 -5.2542e+15 1.094625e+14
df1        2           2           2           
df2        1           1           1           
p.value    1           1           6.758531e-08
conf.level 0.95        0.95        0.95        
lbound     0.005803109 0.005803109 4.823719e-05
ubound     0.9944658   0.9944658   0.5976005   
------------------------------------------------------
usr1$bin:b
           a1          a2          a3          
subjects   2           2           2           
raters     2           2           2           
model      "twoway"    "twoway"    "twoway"    
type       "agreement" "agreement" "agreement" 
unit       "single"    "single"    "single"    
icc.name   "ICC(A,1)"  "ICC(A,1)"  "ICC(A,1)"  
value      0.9         0.9         0.06923077  
r0         0           0           0           
Fvalue     Inf         Inf         Inf         
df1        1           1           1           
df2        1           1           1           
p.value    0           0           0           
conf.level 0.95        0.95        0.95        
lbound     0.01370303  0.01370303  0.0001148084
ubound     0.9998285   0.9998285   0.9796676   

您可以将 lapply 包装成不同的 bin:

res<-lapply(unique(usr1$bin),function(x){  
  mapply(function(x,y){z<-data.frame(x,y);icc(z,model="t",type="a")},usr1[usr1$bin==x,-4],usr2[usr1$bin==x,])
})
names(res)<-unique(usr1$bin)