阅读权限 255 威望 0 级论坛币 2302 个 通用积分 908.3324 学术水平 114 点 热心指数 120 点 信用等级 83 点 经验 52009 点 帖子 1552 精华 1 在线时间 2357 小时 注册时间 2005-1-13 最后登录 2024-5-21
em.txt
(10.03 KB)
原程序.txt
(5.99 KB)
epoh老师,您好!
您看一下下面的程序运行错在什么地方?我尝试修改,还是不能解决问题。
非常感谢!
附件中有程序。
> #######################################################
> ### R code for Lec 10 Examples ###
> #######################################################
>
> ### Example 1 (pepper moth)
>
>
> moth<-function(p,n.obs){
+ n<-sum(n.obs)
+ nc<-n.obs[1]
+ ni<-n.obs[2]
+ nt<-n.obs[3]
+ ntt<-nt
+
+ cat(p,"\n")
+ pct<-pit<-ptt<-rep(0,20)
+ pct[1]<-p[1]
+ pit[1]<-p[2]
+ ptt[1]<-1-p[1]-p[2]
+ for(i in 2:20){
+ pc.old<-pct[i-1]
+ pi.old<-pit[i-1]
+ pt.old<-ptt[i-1]
+
+ den<-pc.old^2+2*pc.old*pi.old+2*pc.old*pt.old
+ ncc<-nc*pc.old^2/den
+ nci<-2*nc*pc.old*pi.old/den
+ nct<-2*nc*pc.old*pt.old/den
+ nii<-ni*pi.old^2/(pi.old^2+2*pi.old*pt.old)
+ nit<-2*ni*pi.old*pt.old/(pi.old^2+2*pi.old*pt.old)
+
+ pct
<-(2*ncc+nci+nct)/(2*n)
+ pit<-(2*nii+nit+nci)/(2*n)
+ ptt<-(2*ntt+nct+nit)/(2*n)
+ }
+ return(list(pct=pct,pit=pit,ptt=ptt))
+ }
>
> n.obs<-c(85,196,341) # observed data,n_c,n_I,n_T
> p<-c(1/3,1/3)
> a<-moth(p,n.obs)
0.3333333 0.3333333
> pct<-a$pct
> pit<-a$pit
> ptt<-a$ptt
> #convergence diagnostics
> # statistic R
> rcc=sqrt( (diff(pct)^2+diff(pit)^2)/(pct[-20]^2+pit[-20]^2) )
> rcc=c(0,rcc) #adjusts the length to make the table below
>
> d1=(pct[-1]-pct[20])/(pct[-20]-pct[20])
> d1=c(d1,0)
> d2=(pit[-1]-pit[20])/(pit[-20]-pit[20])
> d2=c(d2,0)
>
> #Table output
> print(cbind(pct,pit,rcc,d1,d2)[1:9,],digits=5)
pct pit rcc d1 d2
[1,] 0.333333 0.33333 0.0000e+00 0.042502 0.33659
[2,] 0.081994 0.23741 5.7069e-01 0.036933 0.18765
[3,] 0.071249 0.19787 1.6312e-01 0.036727 0.17780
[4,] 0.070852 0.19036 3.5756e-02 0.036719 0.17624
[5,] 0.070837 0.18902 6.5860e-03 0.036719 0.17595
[6,] 0.070837 0.18879 1.1683e-03 0.036719 0.17589
[7,] 0.070837 0.18875 2.0580e-04 0.036719 0.17588
[8,] 0.070837 0.18874 3.6205e-05 0.036720 0.17587
[9,] 0.070837 0.18874 6.3679e-06 0.036736 0.17587
>
> ### Example 2 Bayes EM: mode of posterior
> #generate observed data
> theta=0.3
> n<-50
> x<-drop(rmultinom(1,n,c((2+theta)/4,(1-theta)/2,theta/4)))
> # prior paramters
> a<-.01
> b<-.01
>
>
> th<-rep(0,20)
> th[1]<-0.2
> for(t in 2:20){
+ num<-x[1]*th[t-1]/(2+th[t-1])+x[3]+a-1
+ den<-x[1]*th[t-1]/(2+th[t-1])+x[2]+x[3]+a+b-2
+ th[t]<-num/den
+ }
> rcc<-sqrt( (diff(th)^2+diff(th)^2)/(th[-20]^2+th[-20]^2) )
> print(cbind(th,c(0,rcc)),digits=5)
th
[1,] 0.20000 0.0000e+00
[2,] 0.19297 3.5148e-02
[3,] 0.18928 1.9131e-02
[4,] 0.18732 1.0366e-02
[5,] 0.18627 5.6016e-03
[6,] 0.18570 3.0227e-03
[7,] 0.18540 1.6298e-03
[8,] 0.18524 8.7843e-04
[9,] 0.18515 4.7333e-04
[10,] 0.18510 2.5502e-04
[11,] 0.18508 1.3739e-04
[12,] 0.18506 7.4013e-05
[13,] 0.18506 3.9871e-05
[14,] 0.18505 2.1478e-05
[15,] 0.18505 1.1570e-05
[16,] 0.18505 6.2329e-06
[17,] 0.18505 3.3576e-06
[18,] 0.18505 1.8087e-06
[19,] 0.18505 9.7435e-07
[20,] 0.18505 5.2488e-07
> ### Example 3 SEM algorithm
>
> #First, run EM using the code in the basic EM algorithm given in Example 1.
> #Then we run SEM as follows.
>
> #Starting values for SEM
> #Choosing the parameter values at the 20th iteration of EM.
> pcthat=pct[20]
> pithat=pit[20]
> ptthat=ptt[20]
>
> #Initialize the parameter estimate vectors, one vector for each phenotype
> sempct=rep(0,20)
> sempit=rep(0,20)
> semptt=rep(0,20)
> sempct[1]=0.07 #Initial SEM estimates of the parameters
> sempit[1]=0.19
> semptt[1]=0.74
> rij=array(0,c(3,3,20))
>
> #This for loop implements the SEM algorithm for the peppered moth
> #example.
>
> #Below t is the number of SEM iterations
> for (t in 2:20) {
+ #Take standard EM step
+ denom1=(sempct[t-1]^2+2*sempct[t-1]*sempit[t-1]+2*sempct[t-1]*semptt[t-1])
+ ncct=nc*sempct[t-1]^2/denom1
+ ncit=2*nc*sempct[t-1]*sempit[t-1]/denom1
+ nctt=nc-ncct-ncit
+ niit=ni*sempit[t-1]^2/(sempit[t-1]^2+2*sempit[t-1]*semptt[t-1])
+ nitt=ni-niit
+ nttt=nt
+ sempct[t]=(2*ncct+ncit+nctt)/(2*n)
+ sempit[t]=(2*niit+ncit+nitt)/(2*n)
+ semptt[t]=(2*nttt+nctt+nitt)/(2*n)
+ #SEM
+ #loop over each parameter
+ for (j in 1:3) {
+ #start at estimates from the the 20th iteration of EM
+ sempj=c(pcthat,pithat,ptthat)
+
+ #replace the jth element of sempj with the most recent EM estimate
+ sempj[j]=c(sempct[t],sempit[t],semptt[t])[j]
+
+ #Take one EM step for sempj
+ denom1=(sempj[1]^2+2*sempj[1]*sempj[2]+2*sempj[1]*sempj[3])
+ ncct=nc*sempj[1]^2/denom1
+ ncit=2*nc*sempj[1]*sempj[2]/denom1
+ nctt=nc-ncct-ncit
+ niit=ni*sempj[2]^2/(sempj[2]^2+2*sempj[2]*sempj[3])
+ nitt=ni-niit
+ nttt=nt
+ nextstep=c((2*ncct+ncit+nctt)/(2*n),(2*niit+ncit+nitt)/(2*n),
+ (2*nttt+nctt+nitt)/(2*n))
+
+ # Calculate rij. This is (4.44) on page 101
+ rij[,j,t]=(nextstep-c(pcthat,pithat,ptthat))/
+ (sempj[j]-c(pcthat,pithat,ptthat)[j])
+ }
+ }
错误: 找不到对象'nc'
> for(t in 1:20){
+ cat(t,sempct[t],sempit[t],semptt[t])
+ cat("\n")
+ print(rij[,,t])
+ cat("\n")
+ }
1 0.07 0.19 0.74
[,1] [,2] [,3]
[1,] 0 0 0
[2,] 0 0 0
[3,] 0 0 0
2 0 0 0
[,1] [,2] [,3]
[1,] 0 0 0
[2,] 0 0 0
[3,] 0 0 0
3 0 0 0
[,1] [,2] [,3]
[1,] 0 0 0
[2,] 0 0 0
[3,] 0 0 0
4 0 0 0
[,1] [,2] [,3]
[1,] 0 0 0
[2,] 0 0 0
[3,] 0 0 0
5 0 0 0
[,1] [,2] [,3]
[1,] 0 0 0
[2,] 0 0 0
[3,] 0 0 0
6 0 0 0
[,1] [,2] [,3]
[1,] 0 0 0
[2,] 0 0 0
[3,] 0 0 0
7 0 0 0
[,1] [,2] [,3]
[1,] 0 0 0
[2,] 0 0 0
[3,] 0 0 0
8 0 0 0
[,1] [,2] [,3]
[1,] 0 0 0
[2,] 0 0 0
[3,] 0 0 0
9 0 0 0
[,1] [,2] [,3]
[1,] 0 0 0
[2,] 0 0 0
[3,] 0 0 0
10 0 0 0
[,1] [,2] [,3]
[1,] 0 0 0
[2,] 0 0 0
[3,] 0 0 0
11 0 0 0
[,1] [,2] [,3]
[1,] 0 0 0
[2,] 0 0 0
[3,] 0 0 0
12 0 0 0
[,1] [,2] [,3]
[1,] 0 0 0
[2,] 0 0 0
[3,] 0 0 0
13 0 0 0
[,1] [,2] [,3]
[1,] 0 0 0
[2,] 0 0 0
[3,] 0 0 0
14 0 0 0
[,1] [,2] [,3]
[1,] 0 0 0
[2,] 0 0 0
[3,] 0 0 0
15 0 0 0
[,1] [,2] [,3]
[1,] 0 0 0
[2,] 0 0 0
[3,] 0 0 0
16 0 0 0
[,1] [,2] [,3]
[1,] 0 0 0
[2,] 0 0 0
[3,] 0 0 0
17 0 0 0
[,1] [,2] [,3]
[1,] 0 0 0
[2,] 0 0 0
[3,] 0 0 0
18 0 0 0
[,1] [,2] [,3]
[1,] 0 0 0
[2,] 0 0 0
[3,] 0 0 0
19 0 0 0
[,1] [,2] [,3]
[1,] 0 0 0
[2,] 0 0 0
[3,] 0 0 0
20 0 0 0
[,1] [,2] [,3]
[1,] 0 0 0
[2,] 0 0 0
[3,] 0 0 0
> #Note that the algorithm becomes unstable on 8th iteration
> #Below is the output for iteration 7
> #EM after 20 iterations
> #> cbind(pct,pit,ptt)[20,]
> # pct pit ptt
> #0.07083691 0.18873652 0.74042657
>
> #SEM after 7 iterations (the way the indices work, this is index 6
> #> cbind(sempct,sempit,semptt)[6,]
> # sempct sempit semptt
> #0.07083691 0.18873670 0.74042639
>
> rij[,,6]
[,1] [,2] [,3]
[1,] 0 0 0
[2,] 0 0 0
[3,] 0 0 0
> # [,1] [,2] [,3]
> #[1,] 0.034117920 -0.002601059 -0.00260106
> #[2,] -0.006910223 0.140676982 -0.03519589
> #[3,] -0.027207449 -0.138075923 0.03779695
>
> #Now need iyhat (inverse of the information matrix for Y) used in (4.43)
>
> #Equations (4.5)-(4.9) on page 93
> denom1=(pcthat^2+2*pcthat*pithat+2*pcthat*ptthat)
> ncct=nc*pcthat^2/denom1
错误: 找不到对象'nc'
> ncit=2*nc*pcthat*pithat/denom1
错误: 找不到对象'nc'
> nctt=nc-ncct-ncit
错误: 找不到对象'nc'
> niit=ni*pithat^2/(pithat^2+2*pithat*ptthat)
错误: 找不到对象'ni'
> nitt=ni-niit
错误: 找不到对象'ni'
> nttt=nt
错误: 找不到对象'nt'
>