提前感谢您花时间帮助我解决我的问题。
我正在尝试编写一个程序来计算几个不同组的标准差,最后一步让我有点难过。
data<- read.dta("FinalRfile.dta")
Data<-data
grouplist<-16
grange<-range(Data[grouplist])
groupnum<-grange[2]-grange[1]+1
groupnums<-(1:groupnum)
groupmean<-list()
for (i in groupnums) groupmean[[i]]<-sapply(subset(Data, Data[grouplist] == i),mean)
groupvar<-list()
for (i in groupnums) groupvar[[i]]<-sapply(subset(Data, Data[grouplist] == i),var)
#creating a matrix of the all possible combinations that do not repeat
totcombs<-combn(groupnum,2)
# creating variable for total number of columns, which are total number of combinations
ncombs<-ncol(totcombs)
ncols<-(1:ncombs)
gbias<-list()
# The code below is the section causing the problem
for (i in ncombs) gbias[[i]]<-(groupmean[totcombs[1,i]]-groupmean[totcombs[2,i]])/((groupvar[totcombs[1,i]]-groupvar[totcombs[2,i]])^(.5))
Error in groupmean[totcombs[1, i]] - groupmean[totcombs[2, i]] :
non-numeric argument to binary operator
基本上我正在尝试使用公式“(mean_1-mean_2/sqrt(variance_1-variance_2)) 与列表 groupmean 和 groupvar 中的变量以及所有可能的组合。例如组 1&2,1&3,1&4,1&5,2&4 等。以下是设置列表的示例以提供帮助
>groupmean[1]
[[1]]
zid tookphys black income age female
3.729809e+05 6.303419e-01 1.783806e-01 8.128384e+03 2.498148e+01 5.080128e-01
educdec num mhi disea child xghindx
1.177905e+01 4.027778e+00 7.684203e+01 1.109670e+01 4.209402e-01 7.283730e+01
afairnm ghinnm xghindx2 planlist
9.396368e-01 8.173077e-01 5.531082e+03 1.000000e+00
> totcombs
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 1 1 1 2 2 2 3 3 4
[2,] 2 3 4 5 3 4 5 4 5 5
后注:在人们的帮助下确定。我已经到了它返回值的地步。一些值是 NaN 任何想法。再次感谢。
group1<-totcombs[1,]
group2<-totcombs[2,]
gbias<-list()
> for (i in ncols) gbias[[i]]<-abs(groupmean[[group1[i]]]-groupmean[[group2[i]]])/(sqrt((groupvar[[group1[i]]]+groupvar[[group2[i]]])/2))
>
> gbias
[[1]]
zid tookphys black income age female
0.122500336 0.090412751 0.015108219 0.049922006 0.006736868 0.018827112
educdec num mhi disea child xghindx
0.092545644 0.077577473 0.010293535 0.069193643 0.019816257 0.074608054
afairnm ghinnm xghindx2 planlist
0.029083388 0.133190608 0.063947169 Inf