JiscMail Logo
Email discussion lists for the UK Education and Research communities

Help for BUGS Archives


BUGS Archives

BUGS Archives


BUGS@JISCMAIL.AC.UK


View:

Message:

[

First

|

Previous

|

Next

|

Last

]

By Topic:

[

First

|

Previous

|

Next

|

Last

]

By Author:

[

First

|

Previous

|

Next

|

Last

]

Font:

Proportional Font

LISTSERV Archives

LISTSERV Archives

BUGS Home

BUGS Home

BUGS  August 2016

BUGS August 2016

Options

Subscribe or Unsubscribe

Subscribe or Unsubscribe

Log In

Log In

Get Password

Get Password

Subject:

error variable is not defined in two-level SEM with categorical variables

From:

michela <[log in to unmask]>

Reply-To:

michela <[log in to unmask]>

Date:

Mon, 8 Aug 2016 10:40:59 +0200

Content-Type:

text/plain

Parts/Attachments:

Parts/Attachments

text/plain (259 lines)

Hello,

I'm building a two-level SEM in openBUGS with a mixture of ordered 
categorical, dichotomous and continuous variables. Some of these 
variables contain missing values at random. OpenBUGS is able to handle 
missing values in the continuous variables but crashes if there are 
missing values in the ordered categorical or dichotomous variables.

I used the latent variable approach described in Lee 2007 (Structural 
equation models a Bayesian approach, ch 6 and 7) to handle the 
categorical and dichotomous variables. Each categorical variable is 
assumed to come from a underlying continuous normal variable. In order 
to link the order categorical variable (z) (which contains 3 levels 
k=1,2,3) to the underlying continuous one (y) I defined some threshold 
values (ths) as suggested by Lee: for k=1,2,3  z equal k if 
ths_(k-1)<y<=ths_(k). The dichotomous variables were treated in a 
similar way, but, in this case, there are only two levels, and the 
threshold is set to the value 0. To implement the transformation from 
categorical to continuous variable in openBUGS, I used the censoring 
function I(lower, upper).

I can run the model in openBUGS (3.2.3) through R2OpenBUGS (R version 
3.3.1) only if I do not include missing data in the categorical 
variables. The problem is, I believe, that the censoring function cannot 
handle missing vales. I tried to specify the missingness mechanism for 
the categorical variables, so I created a data frame (rho) of the same 
dimensions of the categorical variable containing 0 and 1. The value of 
1 indicates a missing data point in that position of the data, 0 
otherwise. Then I added the missingness mechanism as follow:

for (j in 1:9){
rho[kk[g]+i,j]~dbern(ber[kk[g]+i,j])
logit(ber[kk[g]+i,j])<-b[1]+b[2]*z[kk[g]+i,1]+b[3]*z[kk[g]+i,2]+
     b[4]*z[kk[g]+i,3]+b[5]*z[kk[g]+i,4]+b[6]*z[kk[g]+i,5]+
     b[7]*z[kk[g]+i,6]+b[8]*z[kk[g]+i,7]+b[9]*z[kk[g]+i,8]+
     b[10]*z[kk[g]+i,9]

}

9 is the number of categorical variables with missing data in the model. 
rho is a data frame of 0 and 1s, z are the categorical data, the row 
specification kk[g]+i is to indicate observation from individual g. It 
is a two-level SEM including for the fact that some individuals in the 
population have repeated observations and the errors are correlated. 
This code produces an error:

"OpenBUGS version 3.2.3 rev 1012 model is syntactically correct

data loaded

variable z is not defined

model must have been compiled but not updated to be able to change RN 
generator"

I also include the full model (see below). y[kk[g]+i,j] are the 
underlying continuous and normally distributed variables. 
I(thd[j,z[kk[g]+i,j]], thd[j,z[kk[g]+i,j]+1]) supplies threshold values 
(thd) to the censoring function for the ordered categorical variables 
and I(low[z[kk[g]+i,j]+1], high[z[kk[g]+i,j]+1]) supplies threshold for 
the dichotomous variable.

Lee provides an example but no code to implement it. I am not sure what 
I am doing wrong. Do you have any idea? Any suggestion would be really 
appreciated!

Thank you in advance for your time

Best wishes, Michela

model{ for(g in 1:339){ # number of individuals for(i in 1:N[g]){ # 
number of observations per individuals

         #missingness mechanism
         for (j in 1:9){
             rho[kk[g]+i,j]~dbern(ber[kk[g]+i,j])
             logit(ber[kk[g]+i,j])<- 
b[1]+b[2]*z[kk[g]+i,1]+b[3]*z[kk[g]+i,2]+b[4]*z[kk[g]+i,3]+b[5]*z[kk[g]+i,4]+b[6]*z[kk[g]+i,5]+b[7]*z[kk[g]+i,6]+b[8]*z[kk[g]+i,7]+b[9]*z[kk[g]+i,8]+b[10]*z[kk[g]+i,9]
         }
         #measurement model
         #ordered categorical variables
         for (j in 1:7){
             y[kk[g]+i,j]~dnorm(u[kk[g]+i,j],1)I(thd[j,z[kk[g]+i,j]], 
thd[j,z[kk[g]+i,j]+1])
             ephat[kk[g]+i,j]<-y[kk[g]+i,j]-u[kk[g]+i,j] # errors, 
epsilon hat
         }
         # dichotomous variables
         for (j in 8:10){
             y[kk[g]+i,j]~dnorm(u[kk[g]+i,j],1)I(low[z[kk[g]+i,j]+1], 
high[z[kk[g]+i,j]+1])
             ephat[kk[g]+i,j]<-y[kk[g]+i,j]-u[kk[g]+i,j] # errors, 
epsilon hat
         }
         #continuous variables
         for (j in 11:20){
             z[kk[g]+i,j]~dnorm(u[kk[g]+i,j],psi[j])
             ephat[kk[g]+i,j]<-z[kk[g]+i,j]-u[kk[g]+i,j] # errors, 
epsilon hat
         }


         u[kk[g]+i,1]<- 
a[1,1]*fc[kk[g]+i,1]+a[1,2]*fc[kk[g]+i,2]+a[1,3]*fc[kk[g]+i,3]+pi[g,1]+eta[g,i] 

         u[kk[g]+i,2]<- 
a[2,1]*fc[kk[g]+i,1]+a[2,2]*fc[kk[g]+i,2]+a[2,3]*fc[kk[g]+i,3]+lb[1]*pi[g,1]+lw[1]*eta[g,i] 

         u[kk[g]+i,3]<- 
a[3,1]*fc[kk[g]+i,1]+a[3,2]*fc[kk[g]+i,3]+a[3,3]*fc[kk[g]+i,3]+lb[2]*pi[g,1]+lw[2]*eta[g,i] 


         u[kk[g]+i,4]<- 
a[4,1]*fc[kk[g]+i,1]+a[4,2]*fc[kk[g]+i,2]+a[4,3]*fc[kk[g]+i,3]+pi[g,4]+xi[g,i,3]
         u[kk[g]+i,5]<- 
a[5,1]*fc[kk[g]+i,1]+a[5,2]*fc[kk[g]+i,2]+a[5,3]*fc[kk[g]+i,3]+lb[3]*pi[g,4]+lw[3]*xi[g,i,3]

         u[kk[g]+i,6]<- 
a[6,1]*fc[kk[g]+i,1]+a[6,2]*fc[kk[g]+i,2]+a[6,3]*fc[kk[g]+i,3]+pi[g,5]+xi[g,i,4]
         u[kk[g]+i,7]<- 
a[7,1]*fc[kk[g]+i,1]+a[7,2]*fc[kk[g]+i,2]+a[7,3]*fc[kk[g]+i,3]+lb[4]*pi[g,5]+lw[4]*xi[g,i,4]
         # dichotomous variables
         u[kk[g]+i,8]<- 
a[8,1]*fc[kk[g]+i,1]+a[8,2]*fc[kk[g]+i,2]+a[8,3]*fc[kk[g]+i,3]+lb[5]*pi[g,1]+lw[5]*eta[g,i]

         u[kk[g]+i,9]<- 
a[9,1]*fc[kk[g]+i,1]+a[9,2]*fc[kk[g]+i,2]+a[9,3]*fc[kk[g]+i,3]+pi[g,3]+xi[g,i,2]
         u[kk[g]+i,10]<- 
a[10,1]*fc[kk[g]+i,1]+a[10,2]*fc[kk[g]+i,2]+a[10,3]*fc[kk[g]+i,3]+lb[6]*pi[g,3]+lw[6]*xi[g,i,2]

         # continuous
         u[kk[g]+i,11]<- 
a[11,1]*fc[kk[g]+i,1]+a[11,2]*fc[kk[g]+i,2]+a[11,3]*fc[kk[g]+i,3]+lb[7]*pi[g,1]+lw[7]*eta[g,i] 


         u[kk[g]+i,12]<- 
a[12,1]*fc[kk[g]+i,1]+a[12,2]*fc[kk[g]+i,2]+a[12,3]*fc[kk[g]+i,3]+pi[g,2]+xi[g,i,1] 
# lambda2 and lambda1set=1
         u[kk[g]+i,13]<- 
a[13,1]*fc[kk[g]+i,1]+a[13,2]*fc[kk[g]+i,2]+a[13,3]*fc[kk[g]+i,3]+lb[8]*pi[g,2]+lw[8]*xi[g,i,1]
         u[kk[g]+i,14]<- 
a[14,1]*fc[kk[g]+i,1]+a[14,2]*fc[kk[g]+i,2]+a[14,3]*fc[kk[g]+i,3]+lb[9]*pi[g,2]+lw[9]*xi[g,i,1]

         u[kk[g]+i,15]<- 
a[15,1]*fc[kk[g]+i,1]+a[15,2]*fc[kk[g]+i,2]+a[15,3]*fc[kk[g]+i,3]+lb[10]*pi[g,3]+lw[10]*xi[g,i,2]
         u[kk[g]+i,16]<- 
a[16,1]*fc[kk[g]+i,1]+a[16,2]*fc[kk[g]+i,2]+a[16,3]*fc[kk[g]+i,3]+lb[11]*pi[g,3]+lw[11]*xi[g,i,2]

         u[kk[g]+i,17]<- 
a[17,1]*fc[kk[g]+i,1]+a[17,2]*fc[kk[g]+i,2]+a[17,3]*fc[kk[g]+i,3]+lb[12]*pi[g,4]+lw[12]*xi[g,i,3]
         u[kk[g]+i,18]<- 
a[18,1]*fc[kk[g]+i,1]+a[18,2]*fc[kk[g]+i,2]+a[18,3]*fc[kk[g]+i,3]+lb[13]*pi[g,4]+lw[13]*xi[g,i,3]

         u[kk[g]+i,19]<- 
a[19,1]*fc[kk[g]+i,1]+a[19,2]*fc[kk[g]+i,2]+a[19,3]*fc[kk[g]+i,3]+lb[14]*pi[g,5]+lw[14]*xi[g,i,4]
         u[kk[g]+i,20]<- 
a[20,1]*fc[kk[g]+i,1]+a[20,2]*fc[kk[g]+i,2]+a[20,3]*fc[kk[g]+i,3]+lb[15]*pi[g,5]+lw[15]*xi[g,i,4]


         xi[g,i,1:4]~dmnorm(ux[1:4],phi[1:4,1:4])      # ux=[0 0]^T is 
fixed constant
         eta[g,i]~dnorm(nu[g,i], psd) #psd e' semplicemente un par from 
dgamma
         nu[g,i]<- gam[1]*xi[g,i,1]+gam[2]*xi[g,i,2]+gam[3]*xi[g,i,3] + 
gam[4]*xi[g,i,4]
         dthat[g,i]<-eta[g,i]-nu[g,i] # questi sono gli errori delta
     }# end of i
     pi[g,1:5]~ dmnorm(uu[1:5],phip[1:5,1:5]) #distributions of omega2 
set mean to zero not sure why
}# end of g

uu[1]<- 0.0    uu[2]<- 0.0   uu[3]<- 0.0   uu[4]<- 0.0 uu[5]<- 0.0
ux[1]<- 0.0    ux[2]<- 0.0   ux[3]<-0.0    ux[4]<-0.0
# priors on loadings and coefficients
a[1,1]~dnorm(0.0,4.0)           a[2,1]~dnorm(-1.0,4.0) 
a[3,1]~dnorm(1.0,4.0)
a[4,1]~dnorm(0.0,4.0)           a[5,1]~dnorm(0.0,4.0) 
a[6,1]~dnorm(-1.0,4.0)
a[7,1]~dnorm(-1.0,4.0)          a[8,1]~dnorm(0.0,4.0) a[9,1]~dnorm(0.0,4.0)
a[10,1]~dnorm(-0.3,4.0)         a[11,1]~dnorm(0.0,4.0) 
a[12,1]~dnorm(1.0,4.0)
a[13,1]~dnorm(-0.5,4.0)         a[14,1]~dnorm(0.0,4.0) 
a[15,1]~dnorm(0.0,4.0)
a[16,1]~dnorm(0.0,4.0)          a[17,1]~dnorm(0.1,4.0) 
a[18,1]~dnorm(1.0,4.0)
a[19,1]~dnorm(0.5,4.0)          a[20,1]~dnorm(0.0,4.0)

a[2,2]~dnorm(1.0,1)             a[3,3]~dnorm(1.0,1) a[8,2]~dnorm(1.0,1)
a[1,2]<- 0.0    a[3,2]<- 0.0    a[4,2]<- 0.0    a[5,2]<- 0.0    a[6,2]<- 
0.0    a[7,2]<- 0.0
a[9,2]<- 0.0    a[10,2]<- 0.0   a[11,2]<- 0.0 a[12,2]<- 0.0   a[13,2]<- 
0.0   a[14,2]<- 0.0
a[15,2]<- 0.0   a[16,2]<- 0.0   a[17,2]<- 0.0 a[18,2]<- 0.0   a[19,2]<- 
0.0   a[20,2]<- 0.0

a[1,3]<- 0.0    a[2,3]<- 0.0    a[4,3]<- 0.0    a[5,3]<- 0.0    a[6,3]<- 
0.0    a[7,3]<- 0.0
a[8,3]<- 0.0    a[9,3]<- 0.0    a[10,3]<- 0.0 a[11,3]<- 0.0   a[12,3]<- 
0.0   a[13,3]<- 0.0
a[14,3]<- 0.0   a[15,3]<- 0.0   a[16,3]<- 0.0 a[17,3]<- 0.0   a[18,3]<- 
0.0   a[19,3]<- 0.0
a[20,3]<- 0.0

var.bw[1]<-4.0*psi[2] var.bw[2]<-4.0*psi[3] var.bw[3]<-4.0*psi[5]
var.bw[4]<-4.0*psi[7] var.bw[5]<-4.0*psi[8] var.bw[6]<-4.0*psi[10]
var.bw[7]<-4.0*psi[11] var.bw[8]<-4.0*psi[13] var.bw[9]<-4.0*psi[14]
var.bw[10]<-4.0*psi[15] var.bw[11]<-4.0*psi[16] var.bw[12]<-4.0*psi[17]
var.bw[13]<-4.0*psi[18] var.bw[14]<-4.0*psi[19] var.bw[15]<-4.0*psi[20]
lb[1]~dnorm(1.0,var.bw[1]) lb[2]~dnorm(1.0,var.bw[2]) 
lb[3]~dnorm(1.0,var.bw[3])
lb[4]~dnorm(1.0,var.bw[4]) lb[5]~dnorm(1.0,var.bw[5]) 
lb[6]~dnorm(1.0,var.bw[6])
lb[7]~dnorm(1.0,var.bw[7]) lb[8]~dnorm(1.0,var.bw[8]) 
lb[9]~dnorm(1.0,var.bw[9])
lb[10]~dnorm(1.0,var.bw[10]) lb[11]~dnorm(1.0,var.bw[11]) 
lb[12]~dnorm(1.0,var.bw[12])
lb[13]~dnorm(1.0,var.bw[13]) lb[14]~dnorm(1.0,var.bw[14]) 
lb[15]~dnorm(1.0,var.bw[15])

lw[1]~dnorm(0.0,var.bw[1])        lw[2]~dnorm(0.0,var.bw[2]) 
lw[3]~dnorm(0.8,var.bw[3])
lw[4]~dnorm(2.6,var.bw[4])        lw[5]~dnorm(3.0,var.bw[5]) 
lw[6]~dnorm(1.0,var.bw[6])
lw[7]~dnorm(0.5,var.bw[7])        lw[8]~dnorm(-0.2,var.bw[8]) 
lw[9]~dnorm(0.0,var.bw[9])
lw[10]~dnorm(1.0,var.bw[10])      lw[11]~dnorm(0.0,var.bw[11]) 
lw[12]~dnorm(0.0,var.bw[12])
lw[13]~dnorm(0.9,var.bw[13])      lw[14]~dnorm(0.3,var.bw[14]) 
lw[15]~dnorm(0.0,var.bw[15])

var.gam<-4.0*psd
gam[1]~dnorm(0.1,var.gam)       gam[2]~dnorm(-0.1,var.gam)
gam[3]~dnorm(0.0,var.gam)       gam[4]~dnorm(0.0,var.gam)
# priors on precisions
for(j in 1:20){psi[j]~dgamma(10.0,4.0)
                 ivpsi[j]<-1/psi[j]}
psd~dgamma(10.0,4.0)
ivpsd<-1/psd
phi[1:4,1:4]~dwish(R0[1:4,1:4],5)
phx[1:4,1:4]<-inverse(phi[1:4,1:4])
phip[1:5,1:5]~dwish(R1[1:5,1:5],5)
php[1:5,1:5]<-inverse(phip[1:5,1:5])

     #priors missingness mechanism
b[1]~dnorm(0,0.1)         b[2]~dnorm(0,0.1) b[3]~dnorm(0,0.1)
  b[4]~dnorm(0,0.1)          b[5]~dnorm(0,0.1) b[6]~dnorm(0,0.1)
  b[7]~dnorm(0,0.1)         b[8]~dnorm(0,0.1) b[9]~dnorm(0,0.1)

}# end of model

-------------------------------------------------------------------
This list is for discussion of modelling issues and the BUGS software.
For help with crashes and error messages, first mail [log in to unmask]
To mail the BUGS list, mail to [log in to unmask]
Before mailing, please check the archive at www.jiscmail.ac.uk/lists/bugs.html
Please do not mail attachments to the list.
To leave the BUGS list, send LEAVE BUGS to [log in to unmask]
If this fails, mail [log in to unmask], NOT the whole list

Top of Message | Previous Page | Permalink

JiscMail Tools


RSS Feeds and Sharing


Advanced Options


Archives

November 2019
October 2019
September 2019
August 2019
July 2019
June 2019
May 2019
April 2019
March 2019
February 2019
January 2019
November 2018
October 2018
September 2018
August 2018
July 2018
June 2018
May 2018
April 2018
March 2018
February 2018
January 2018
December 2017
November 2017
October 2017
September 2017
August 2017
July 2017
May 2017
April 2017
March 2017
February 2017
January 2017
December 2016
November 2016
October 2016
September 2016
August 2016
July 2016
June 2016
May 2016
April 2016
March 2016
February 2016
January 2016
December 2015
November 2015
October 2015
September 2015
August 2015
July 2015
June 2015
May 2015
April 2015
March 2015
February 2015
January 2015
December 2014
November 2014
October 2014
September 2014
August 2014
July 2014
June 2014
May 2014
April 2014
March 2014
February 2014
January 2014
December 2013
November 2013
October 2013
September 2013
August 2013
July 2013
June 2013
May 2013
April 2013
March 2013
February 2013
January 2013
December 2012
November 2012
October 2012
September 2012
August 2012
July 2012
June 2012
May 2012
April 2012
March 2012
February 2012
January 2012
December 2011
November 2011
October 2011
September 2011
August 2011
July 2011
June 2011
May 2011
April 2011
March 2011
February 2011
January 2011
December 2010
November 2010
October 2010
September 2010
August 2010
July 2010
June 2010
May 2010
April 2010
March 2010
February 2010
January 2010
December 2009
November 2009
October 2009
September 2009
August 2009
July 2009
June 2009
May 2009
April 2009
March 2009
February 2009
January 2009
December 2008
November 2008
October 2008
September 2008
August 2008
July 2008
June 2008
May 2008
April 2008
March 2008
February 2008
January 2008
December 2007
November 2007
October 2007
September 2007
August 2007
July 2007
June 2007
May 2007
April 2007
March 2007
February 2007
January 2007
2006
2005
2004
2003
2002
2001
2000
1999
1998


JiscMail is a Jisc service.

View our service policies at https://www.jiscmail.ac.uk/policyandsecurity/ and Jisc's privacy policy at https://www.jisc.ac.uk/website/privacy-notice

Secured by F-Secure Anti-Virus CataList Email List Search Powered by the LISTSERV Email List Manager