[R] Help with SEM: "Error in 1:m : NA/NaN argument"
John Fox
jfox at McMaster.ca
Tue Feb 11 18:11:24 CET 2014
Dear Annalisa,
> -----Original Message-----
> From: Annalisa Stacchini [mailto:s.annina at hotmail.it]
> Sent: Tuesday, February 11, 2014 10:29 AM
> To: John Fox
> Subject: RE: [R] Help with SEM: "Error in 1:m : NA/NaN argument"
>
>
> Kind Prof. Fox,
> thanks for your attention,
>
>
> 1) to read my datasets you have to write: CC32<-
> read.table("CC32.Rdata") and if you cannot, it depends on the version
> of R you are using, so I attach the same file but in csv format (CC32<-
> read.csv("CC32.csv").
Files with extension .Rdata or .Rda are conventionally in an "internal"
format, to be read by load(). I'm not sure how I could have known that
CC32.Rdata contained text to be read by read.table(). Anyway, I now can read
your data (after fixing the first row with variable names).
>
> 2) all of the variables in the model are observed variables, infact
> there is no measurement model. I use sem methodology because I have
> ordinal variables, dichotomus and continuous variables, so that the
> only latent variables are the continuous variables underlying the
> cathegorical ones. As it is well known, sem with poliserial, polichoric
> and pearson correlations as input is equivalent to a system of linear
> and logit/probit regressions.
>
> 3) therefore I use GLS objective, to get a linear link function for
> non-normal ordinal variables ( the usual methodology for cathegorical
> data)
>
> 3) I cannot input raw data instead of the correlation matrix, because R
> does not allow me to directly specify the type of correlation it should
> compute with different variables (poliserial, polichoric, pearson).
>
> 4) parameters set to zero are necessary for the model to be identified
> and are consistent with the theory, in particular they are necessary
> for the model to satisfy the rank condition.
I'm afraid that I didn't make myself clear: You haven't set these parameters
to the value 0, you've given them the name "0". To set them to zero, you'd
need something like x -> y, NA, 0. But that's unnecessary, since if you
simply leave out the path, it's automatically fixed to 0.
Also, because your model implicitly sets all error covariances to 0, the
usual rank condition doesn't apply. Of course, these additional constraints
serve to overidentify the model further, but I wonder whether they are
plausible.
There is also another error in your model specification: you have no
parameter associated with the path SpesaMediaG-> VOTO_PREZZI. I'll reiterate
my suggestion that specifyEquations() is easier to use than specifyModel().
When I fix the problems (though I've not addressed the error covariances), I
get
----------- snip ----------
Mod.Bus<- specifyModel()
Dipendente->VOTO_ARTE,dipart
Dipendente->VOTO_AMBIENTE,dipamb
Dipendente->VOTO_ALBERGHI,dipalb
Dipendente->VOTO_PASTI,dippas
Dipendente->VOTO_PREZZI,dippre
Dipendente->VOTO_ACQUISTI,dipacq
Dipendente->VOTO_INFORMAZIONI,dipinf
Dipendente->VOTO_SICUREZZA,dipsic
SESSO->VOTO_CORTESIA,Mcort
SESSO->VOTO_ARTE,Mart
SESSO->VOTO_AMBIENTE,Mamb
SESSO->VOTO_ALBERGHI,Malb
SESSO->VOTO_PASTI,Mpas
SESSO->VOTO_PREZZI,Mpre
SESSO->VOTO_SICUREZZA,Msic
ETA->VOTO_CORTESIA,etacort
ETA->VOTO_ARTE,etart
ETA->VOTO_AMBIENTE,etaamb
ETA->VOTO_PASTI,etapas
ETA->VOTO_PREZZI,etapre
ETA->VOTO_ACQUISTI,etaacq
ETA->VOTO_SICUREZZA,etasic
Europa->VOTO_CORTESIA,eucort
Europa->VOTO_AMBIENTE,euamb
Europa->VOTO_ALBERGHI,eualb
Europa->VOTO_PASTI,eupas
Europa->VOTO_PREZZI,eupre
Europa->VOTO_INFORMAZIONI,euinfo
Europa->VOTO_SICUREZZA,eusic
NordA->VOTO_CORTESIA,nordacort
NordA->VOTO_ARTE,nordaart
NordA->VOTO_ALBERGHI,nordaalb
NordA->VOTO_PASTI,nordapas
NordA->VOTO_PREZZI,nordapre
NordA->VOTO_ACQUISTI,nordaacq
NordA->VOTO_SICUREZZA,ordasi
SudA->VOTO_CORTESIA,sudcort
SudA->VOTO_ARTE,sudrt
SudA->VOTO_AMBIENTE,sudamb
SudA->VOTO_ALBERGHI,sudlb
SudA->VOTO_PREZZI,sudpre
SudA->VOTO_ACQUISTI,sudacq
SudA->VOTO_INFORMAZIONI,sudinf
SudA->VOTO_SICUREZZA,sudsic
Asia->VOTO_CORTESIA,asiacort
Asia->VOTO_CORTESIA,asiacort
Asia->VOTO_ARTE,asiart
Asia->VOTO_AMBIENTE,asiaamb
Asia->VOTO_ALBERGHI,asialb
Asia->VOTO_PASTI,asiapas
Asia->VOTO_PREZZI,asiapre
Asia->VOTO_ACQUISTI,asiaacq
AltroStato->VOTO_ARTE,astatort
AltroStato->VOTO_AMBIENTE,astatoamb
AltroStato->VOTO_PASTI,astatopas
AltroStato->VOTO_PREZZI,astatopre
AltroStato->VOTO_SICUREZZA,astatosic
Strada->VOTO_AMBIENTE,stramb
Strada->VOTO_ALBERGHI,stralb
Strada->VOTO_PASTI,strpas
Strada->VOTO_PREZZI,strpre
Strada->VOTO_ACQUISTI,stracq
Strada->VOTO_INFORMAZIONI,strinf
Strada->VOTO_SICUREZZA,strsic
Aereo->VOTO_CORTESIA, aercort
Aereo->VOTO_ARTE,aerart
Aereo->VOTO_PASTI,aerpas
Aereo->VOTO_PREZZI,aerpre
Aereo->VOTO_ACQUISTI,aeracq
Aereo->VOTO_INFORMAZIONI,aerinf
Aereo->VOTO_SICUREZZA,aersic
AltroMezzo->VOTO_AMBIENTE, amezzoamb
AltroMezzo->VOTO_PREZZI,amezzopre
AltroMezzo->VOTO_ACQUISTI,amezzoacq
AltroMezzo->VOTO_INFORMAZIONI,amezzoinf
AltroMezzo->VOTO_SICUREZZA,amezzosic
NR_NOTTI_X_PROVINCIA->VOTO_ARTE,nottart
NR_NOTTI_X_PROVINCIA->VOTO_AMBIENTE,nottamb
NR_NOTTI_X_PROVINCIA->VOTO_ALBERGHI,nottalb
NR_NOTTI_X_PROVINCIA->VOTO_PASTI,nottpas
NR_NOTTI_X_PROVINCIA->VOTO_ACQUISTI,nottacq
NR_NOTTI_X_PROVINCIA->VOTO_INFORMAZIONI,nottinf
NR_NOTTI_X_PROVINCIA->VOTO_SICUREZZA,nottsic
SpesaMediaG-> VOTO_ALBERGHI, spesalb
VOTO_CORTESIA -> VOTO_COMPLESSIVO,CORTESIAcomp
VOTO_ARTE-> VOTO_COMPLESSIVO,ARTEcomp
VOTO_AMBIENTE->VOTO_COMPLESSIVO,AMBIENTEcomp
VOTO_ALBERGHI->VOTO_COMPLESSIVO,ALBERGHIcomp
VOTO_PASTI-> VOTO_COMPLESSIVO,PASTIcomp
VOTO_PREZZI-> VOTO_COMPLESSIVO,PREZZIcomp
VOTO_ACQUISTI-> VOTO_COMPLESSIVO,ACQUISTIcomp
VOTO_INFORMAZIONI-> VOTO_COMPLESSIVO,INFORMAZIONIcomp
VOTO_SICUREZZA->VOTO_COMPLESSIVO,SICUREZZAcomp
T1->VOTO_COMPLESSIVO, t1comp
Inverno->VOTO_COMPLESSIVO,invcomp
Primavera->VOTO_COMPLESSIVO,primacomp
Estate->VOTO_COMPLESSIVO,estacomp
Autunno->VOTO_COMPLESSIVO,autcomp
VOTO_COMPLESSIVO-> SpesaMediaG,comp_spesa
SpesaMediaG-> VOTO_COMPLESSIVO, spesa_comp
T1 ->SpesaMediaG,t1spe
SESSO ->SpesaMediaG,Mpe
ETA ->SpesaMediaG,etaspe
Europa ->SpesaMediaG,euspe
NordA ->SpesaMediaG,nordaspe
SudA ->SpesaMediaG,sudspe
Asia ->SpesaMediaG,asiaspe
AltroStato ->SpesaMediaG,astatospe
Primavera ->SpesaMediaG,primspe
Estate ->SpesaMediaG,estspe
Autunno ->SpesaMediaG,autspe
Inverno ->SpesaMediaG,invspe
ETA-> VOTO_COMPLESSIVO, etacomp
NR_NOTTI_X_PROVINCIA-> VOTO_COMPLESSIVO, notticomp
SpesaMediaG-> VOTO_PREZZI, B
T1-> VOTO_PASTI, t1pas
T1->VOTO_PREZZI, t1prez
SpesaMediaG->VOTO_ARTE, speart
VOTO_INFORMAZIONI-> VOTO_AMBIENTE, InfAmb
VOTO_CORTESIA-> VOTO_ALBERGHI, CortAlb
VOTO_CORTESIA -> VOTO_ACQUISTI, Cort_Acq
VOTO_ACQUISTI-> VOTO_CORTESIA, Acq_Cort
VOTO_CORTESIA-> VOTO_PASTI, CortPas
VOTO_CORTESIA-> VOTO_INFORMAZIONI, CortInf
VOTO_CORTESIA-> VOTO_SICUREZZA, CortSic
VOTO_ACQUISTI->VOTO_PREZZI, Acq_Prez
VOTO_PREZZI-> VOTO_ACQUISTI,Prez_Acq
VOTO_PASTI-> VOTO_ALBERGHI, PasAlb
VOTO_PREZZI-> VOTO_ALBERGHI, Prez_Alb
VOTO_ALBERGHI-> VOTO_PREZZI, Alb_Prez
VOTO_PREZZI-> VOTO_PASTI, PrezPas
VOTO_INFORMAZIONI-> VOTO_ACQUISTI, InfAcq
---------------------- snip ----------------
This model runs without error, but doesn't provide a reasonable solution:
---------------------- snip ----------------
> CLB <- classifyVariables(Mod.Bus)
> fisse<-as.character(CLB$exogenous)
> Business32<-sem(Mod.Bus,
+ S=CC32, N=16029, fixed.x=fisse, objective= objectiveGLS,
+ maxiter=100000)
Warning messages:
1: In eval(expr, envir, enclos) :
Optimization may not have converged; nlm return code = 3. Consult ?nlm.
2: In eval(expr, envir, enclos) : Negative parameter variances.
Model may be underidentified.
> Business32
Model Chisquare = 338689.2 Df = 111
dipart dipamb dipalb
dippas dippre dipacq dipinf
dipsic
-7.459102e-02 -1.034447e-01 -9.294233e-02
2.261100e-02 1.173030e-02 -8.947510e-02 1.934227e-03
-7.559449e-02
Mcort Mart Mamb
Malb Mpas Mpre Msic
etacort
9.675428e-02 -9.377040e-02 2.379614e-01
1.561788e+00 4.155558e-01 1.418167e+00 -1.434057e-01
8.814979e-02
etart etaamb etapas
etapre etaacq etasic eucort
euamb
2.563779e-01 8.511191e-03 -1.320395e-01
-1.766002e-01 9.218092e-02 -1.551017e-01 2.392661e-01
-8.686180e-01
eualb eupas eupre
euinfo eusic nordacort nordaart
nordaalb
3.452997e+00 -3.937365e-01 7.165134e+01
4.745325e-01 1.281827e+00 7.053228e-01 -3.910060e-01
-1.448172e-01
nordapas nordapre nordaacq
ordasi sudcort sudrt sudamb
sudlb
-9.069084e-02 4.911247e+01 -1.358857e-01
2.438344e-01 7.791269e-01 7.209359e-02 2.521498e-01
-3.995438e-01
sudpre sudacq sudinf
sudsic asiacort asiart asiaamb
asialb
2.510991e+01 1.814347e-01 -1.166736e-01
3.990721e-01 1.506532e-02 -1.641671e-02 6.563086e-01
-2.083407e+00
asiapas asiapre asiaacq
astatort astatoamb astatopas astatopre
astatosic
-4.752590e-01 3.997544e+01 4.824307e-01
-7.259909e-02 1.218808e-01 -4.179664e-01 3.649246e+01
1.586175e+00
stramb stralb strpas
strpre stracq strinf strsic
aercort
7.143525e-01 -5.783230e-02 5.356335e-02
-2.716246e+02 2.484341e+02 1.119892e+02 4.920591e+01
-5.577094e-01
aerart aerpas aerpre
aeracq aerinf aersic amezzoamb
amezzopre
2.372531e-02 3.832866e-01 -4.193232e+02
3.839778e+02 1.729901e+02 7.537199e+01 5.020861e-02
-3.237979e+02
amezzoacq amezzoinf amezzosic
nottart nottamb nottalb nottpas
nottacq
2.961828e+02 1.329671e+02 5.780849e+01
6.227002e-02 -5.079648e-01 1.392114e-01 -1.407430e-03
1.350893e-01
nottinf nottsic spesalb
CORTESIAcomp ARTEcomp AMBIENTEcomp ALBERGHIcomp
PASTIcomp
1.164640e-01 1.102893e+00 -6.082652e+00
5.377620e-01 3.072100e-01 -5.270727e-01 -2.699380e+00
1.321595e+00
PREZZIcomp ACQUISTIcomp INFORMAZIONIcomp
SICUREZZAcomp t1comp invcomp primacomp
estacomp
3.880983e+00 4.193467e+00 3.290604e+00
1.005019e+01 -1.816556e-01 -1.094417e+02 -1.126156e+02
-1.010769e+02
autcomp comp_spesa spesa_comp
t1spe Mpe etaspe euspe
nordaspe
-1.114783e+02 -2.351864e+00 -4.959690e+00
-2.115910e-01 2.202224e-01 -5.721944e-02 -4.018005e+00
-3.552802e+00
sudspe asiaspe astatospe
primspe estspe autspe invspe
etacomp
-1.952452e+00 -4.673669e+00 -1.839954e+00
3.322502e+01 2.969667e+01 3.281346e+01 3.220890e+01
-3.806601e-02
notticomp B t1pas
t1prez speart InfAmb CortAlb
Cort_Acq
3.477507e-01 -5.318109e+00 1.041311e-01
8.968226e-02 6.762492e-01 3.287031e+00 2.277209e+00
2.426343e+00
Acq_Cort CortPas CortInf
CortSic Acq_Prez Prez_Acq PasAlb
Prez_Alb
-3.705727e+00 1.675947e-01 1.591227e-01
-2.793449e+00 3.189496e+00 5.134531e+00 2.150903e-01
5.356811e+00
Alb_Prez PrezPas InfAcq
V[VOTO_ARTE] V[VOTO_AMBIENTE] V[VOTO_ALBERGHI] V[VOTO_PASTI]
V[VOTO_PREZZI]
-1.477526e+00 4.275359e-01 -1.665437e-01
-1.423403e-01 2.273039e-01 4.984140e-02 -2.413120e-02
-9.187588e-03
V[VOTO_ACQUISTI] V[VOTO_INFORMAZIONI] V[VOTO_SICUREZZA]
V[VOTO_CORTESIA] V[SpesaMediaG] V[VOTO_COMPLESSIVO]
-3.167497e-02 -2.749050e-02 -4.631287e+00
3.683333e-02 6.663993e-03 -4.598814e+00
Iterations = 746
----------- snip ----------
I'm copying this response to r-help, where the discussion started -- that's
usually a good idea.
Best,
John
>
>
> 5) don't be concerned with the number of iterations, the algorithm is
> never iterated as many times, but it's just a trick to try to
> understand which is the real error (sometimes R prints error messagges
> that are misleading and one of these in one of my tries was "maximum
> number of iteration exceeded", but it was not true, as I checked all
> the passagges it did). However you can cancel maxiter, nothing changes.
>
> I hope now my problem is clearer and somebody can help me.
> Thanks
> Annalisa
>
>
> > From: jfox at mcmaster.ca
> > To: s.annina at hotmail.it
> > CC: r-help at r-project.org
> > Subject: RE: [R] Help with SEM: "Error in 1:m : NA/NaN argument"
> > Date: Tue, 11 Feb 2014 10:07:31 -0500
> >
> > Dear Annalisa,
> >
> > I was unable to load either of the data sets that you provided,
> obtaining
> > the following message:
> >
> > ---------- snip --------
> >
> > > load(file.choose())
> > Error: bad restore file magic number (file may be corrupted) -- no
> data
> > loaded
> > In addition: Warning message:
> > file 'CC32.Rdata' has magic number '"T1" '
> > Use of save versions prior to 2 is deprecated
> >
> > ---------- snip --------
> >
> > I assume from what you did that all of the exogenous variables in the
> model
> > are observed variables in the covariance matrix in CC32; otherwise it
> > wouldn't make sense to specify them as fixed.x.
> >
> > Some other comments: You've named a number of parameters "0", with
> the side
> > effect that they are all constrained equal. One can do this, but I
> doubt
> > whether you intended it. I'd encourage you to use specifyEquations(),
> which
> > I think is less error-prone, rather than specifyModel(), and to pass
> the
> > data, rather than a covariance matrix, to sem(). As well, I'd be
> concerned
> > about a model that really required 100000 iterations to converge, and
> wonder
> > why you're using the GLS objective function.
> >
> > I hope this helps,
> > John
> >
> > -----------------------------------------------
> > John Fox, Professor
> > McMaster University
> > Hamilton, Ontario, Canada
> > http://socserv.socsci.mcmaster.ca/jfox/
> >
> >
> >
> > > -----Original Message-----
> > > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> > > project.org] On Behalf Of AnnalisaStacchini
> > > Sent: Monday, February 10, 2014 1:08 PM
> > > To: r-help at r-project.org
> > > Subject: [R] Help with SEM: "Error in 1:m : NA/NaN argument"
> > >
> > > Hi, I am trying to fit a SEM model (the ram specification of the
> model,
> > > along with the estimated polycoric VCmatrix (CC32), is attacched),
> but
> > > I get the following error message: "Error in 1:m : NA/NaN
> argument"I
> > > did not find any help on internet, so I tried to understend what is
> > > going wrong calling: getAnywhere(sem.default) and reproducing the
> > > sem.default function step by step.I found that the problem arises
> from:
> > > n.fix <- length(fixed.x)
> > >
> > > if (!is.null(fixed.x)) {
> > >
> > > for (i in 1:n.fix) {
> > >
> > > for (j in 1:i) {
> > >
> > > ram <- rbind(ram, c(2, fixed.x[i], fixed.x[j],
> > >
> > > 0, S[fixed.x[i],
> > > fixed.x[j]]))
> > >
> > > }
> > >
> > > }
> > >
> > > }
> > >
> > > m <- max(ram[, c(2, 3)])
> > > Indeed, if I run just:
> > > for (i in 1:n.fix) {for (j in 1:i) {G<-c(2, fixed.x[i], fixed.x[j],
> > > 0, S[fixed.x[i], fixed.x[j]]) }
> > > I get only this (G=): "2" "Autunno" "Autunno" "0" "1"
> > > But I still cannot understand what is wrong.The code I used is:
> > > library(sem)CLB<-classifyVariables(Mod.Bus)
> > >
> > > fisse<-as.character(CLB$exogenous)Business32<-sem(Mod.Bus,
> > > S= as.matrix(CC32), N<- 16029, fixed.x=fisse,objective=
> objectiveGLS,
> > > maxiter=100000)
> > >
> > > Can anybody help me please?Any suggestion will be truly
> > > appreciated.Annalisa
> > >
> > >
> > > Model ram specific.docx (21K)
> > >
> <http://r.789695.n4.nabble.com/attachment/4685052/0/Model%20ram%20speci
> > > fic.docx>
> > > CC32.Rdata (20K)
> > > <http://r.789695.n4.nabble.com/attachment/4685052/1/CC32.Rdata>
> > > C32.Rdata (17K)
> > > <http://r.789695.n4.nabble.com/attachment/4685052/2/C32.Rdata>
> > >
> > >
> > >
> > >
> > > --
> > > View this message in context: http://r.789695.n4.nabble.com/Help-
> with-
> > > SEM-Error-in-1-m-NA-NaN-argument-tp4685052.html
> > > Sent from the R help mailing list archive at Nabble.com.
> > > [[alternative HTML version deleted]]
> > >
> > > ______________________________________________
> > > R-help at r-project.org mailing list
> > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > PLEASE do read the posting guide http://www.R-project.org/posting-
> > > guide.html
> > > and provide commented, minimal, self-contained, reproducible code.
> >
More information about the R-help
mailing list