[R] Problems with Z in rhierMnlRwMixture using bayesm
ea819
ea304 at cam.ac.uk
Thu Aug 4 09:34:31 CEST 2011
Dear All,
I am using rhierMnlRwMixture in the bayesm package for the analysis of data
from a choice experiment. I am trying to follow the margarine example set
out in the bayesm manual (p.28). However, after several attempts I keep
getting an error message with regards to my Z matrix as below.
> Error in Z %*% t(matrix(olddelta, ncol = nz)) :
>requires numeric/complex matrix/vector arguments
I think the problem is arising from the following lines in the code that I
am running.
>Z=NULL
>nlgt=length(lgtdata)
>for(i in 1:nlgt) {
>Z=rbind(Z,Demog[Demog[,1]==lgtdata[[i]]$id,3:9])
I am creating a Z matrix from the variables included in the “Demog”
spreadsheet columns 3 to 9 which includes the demographic variables of the
respondents. Then the rbind matches these columns with the ids in the
lgtdata (which contains the choice attributes). I am not sure if I am
interpreting this code correctly or if I need to amend it. Can anyone help
me with the interpretation of the below and whether I would need to specify
it differently?
Also below is the full code that I am running.
select= c(1,2,3)
id=levels(as.factor(ChoiceAtt[,1]))
lgtdata=NULL
nlgt=length(id)
p=length(select)
ind=1
for (i in 1:nlgt) {
nobs=sum(ChoiceAtt[,1]==id[i])
data=ChoiceAtt
y=data[,2]
names(y)=NULL
X=createX(p=p,na=1,Xa=data[,3:5],nd=NULL,Xd=NULL,INT=TRUE,base=1)
lgtdata[[ind]]=list(y=y,X=X,id=id[i]); ind=ind+1
}
nlgt=length(lgtdata)
Z=NULL
nlgt=length(lgtdata)
for(i in 1:nlgt) {
Z=rbind(Z,Demog[Demog[,1]==lgtdata[[i]]$id,3:9])
}
Z=log(Z)
Z[,1]=Z[,1]-mean(Z[,1])
Z[,2]=Z[,2]-mean(Z[,2])
keep=5
R=5000
mcmc1=list(keep=keep,R=R)
out=rhierMnlRwMixture(Data=list(p=p,lgtdata=lgtdata,Z=Z),Prior=list(ncomp=1),Mcmc=mcmc1)
Many thanks for all your help and time.
Regards,
Elcin
--
View this message in context: http://r.789695.n4.nabble.com/Problems-with-Z-in-rhierMnlRwMixture-using-bayesm-tp3717887p3717887.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list