library(CDM)
alpha=matrix(c(0,0,0,1,0,0,1,1,0,1,1,1),4,3,by=1)
NPC=alpha[sample(1:nrow(alpha),10,replace=T),]
N=10
Q=matrix(c(1,0,0,1,1,0),2,3,by=1)
# repeat
# {
all_eta_w=matrix(0,nrow=nrow(alpha),ncol=nrow(Q))
for(j in 1:nrow(Q))
{
ORP<-function(N)
{
guess=slip=0.1
responses=matrix(0,nrow = N,ncol = nrow(t(Q[j,])))
for(examinee in 1:N)
{
for(item in 1:nrow(t(Q[j,])))
{
responses=CDM::sim.din(N=N,t(Q[j,]),slip=slip,guess=guess,rule="DINA")$dat
}
}
rownames(responses)=c(paste0("examinee",1:N))
colnames(responses)=c(paste0("item",1:nrow(t(Q[j,]))))
return(responses)
}
ORP=ORP(N)
print(ORP)
#属于某种m分类被试的ORP
ex_c1=c()
ex_c2=c()
ex_c3=c()
ex_c4=c()
for(idx in 1:nrow(NPC))
{
if(NPC[idx,][1]==0 && NPC[idx,][2]==0 && NPC[idx,][3]==0)
{
ex_c1[idx]=idx
} else if(NPC[idx,][1]==1 && NPC[idx,][2]==0 && NPC[idx,][3]==0)
{
ex_c2[idx]=idx
} else if(NPC[idx,][1]==1 && NPC[idx,][2]==1 && NPC[idx,][3]==0)
{
ex_c3[idx]=idx
} else if(NPC[idx,][1]==1 && NPC[idx,][2]==1 && NPC[idx,][3]==1)
{
ex_c4[idx]=idx
}
}
#每种cl的人数
c1=length(na.omit(ex_c1))
c2=length(na.omit(ex_c2))
c3=length(na.omit(ex_c3))
c4=length(na.omit(ex_c4))
renshu=t(t(c(c1,c2,c3,c4)))
print(renshu)
#属于Cm分类的被试的ORP
examinee_c1=as.vector(ORP[na.omit(ex_c1),])
examinee_c2=as.vector(ORP[na.omit(ex_c2),])
examinee_c3=as.vector(ORP[na.omit(ex_c3),])
examinee_c4=as.vector(ORP[na.omit(ex_c4),])
Examinee <- list(examinee_c1, examinee_c2, examinee_c3, examinee_c4)
examinee <- do.call(cbind,
lapply(lapply(Examinee, unlist), `length<-`,
max(lengths(Examinee))))
examinee[is.na(examinee)] = NA
examinee=t(examinee)
print(examinee)
#理想反应
eta_c=matrix(0,nrow=nrow(alpha), ncol = nrow(t(Q[j,])))
eta_d=eta_c
for(i in 1:nrow(alpha))
{
eta_c[i,]=apply(t(Q[j,]),1,function(x) all(x<=alpha[i,]))
eta_d[i,]=apply(t(Q[j,]),1,function(x) any(x<=alpha[i,]))
if(alpha[i,][1]==0 &&alpha[i,][2]==0 && alpha[i,][3]==0)
{
eta_c[i,]=eta_d[i,]=c(0)
}else if(alpha[i,][1]==1 &&alpha[i,][2]==1 && alpha[i,][3]==1)
{
eta_c[i,]=eta_d[i,]=c(1)
}
}
print(eta_c)
print(eta_d)
#weight
weight=matrix(0,nrow=nrow(examinee),ncol=1)
eta_w=weight
a=matrix(0,nrow=nrow(examinee),ncol=ncol(examinee))
for(idx in 1:nrow(examinee))
{
a[idx,]=examinee[idx,]-eta_d[idx,]
}
a[is.na(a)] = 0
fenzi=t(t(rowSums(a)))
print(fenzi)
eta<-function(A,B,C,D)
{
for(m in 1:nrow(A))
{
a=A[m,]
b=B[m,]
c=C[m,]
d=D[m,]
if(a==1&&b==1)
{
eta_w[m]=1
}else if(a==0&&b==0)
{
eta_w[m]=0
}else
{
weight[m,]=d/(c*(a-b))
eta_w[m,]=b+weight[m,]*(a-b)
}
}
return(eta_w)
}
all_eta_w[,j]=eta(eta_c,eta_d,renshu,fenzi)
}
print(all_eta_w)
输出的结果是这样的:
其实他把每一次的循环结果都可以打印出来,我在最开始的第一行写了个总的空集,可以求出其中一个变量的最终储存后的循环结果,但是ORP这个变量,我用了同样的方法储存,报错了:说clousure不能得出子集。。
有大神可以帮忙一下吗???求助!!
不知道你这个问题是否已经解决, 如果还没有解决的话: