An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121116/9211405d/attachment.pl>
polycor package
5 messages · Laura Maria Schwirz, John Fox, yrosseel
Dear Laura, As I explained to you when you wrote to me directly, you're not having trouble with the polycor package, since you have AFAICS successfully computed polychoric correlation among your variables. The error is produced when you call sem(), apparently in the lavaan package (though you don't mention that), and you should address your question to the author of that package (or use an appropriate title line in your r-help posting). Best, John ------------------------------------------------ John Fox Sen. William McMaster Prof. of Social Statistics Department of Sociology McMaster University Hamilton, Ontario, Canada http://socserv.mcmaster.ca/jfox/ On Fri, 16 Nov 2012 12:58:03 +0000
Laura Maria Schwirz <schwirzl at tcd.ie> wrote:
I am currently working with R's polycor package and I have encountered a
problem. I tried to follow the steps as outlined in the sem.pdf file where
a CFA model is run using polychoric correlations. Every time I run the
command sem(model, data, N=.), I get the following warning message:
Warning message:
In if (orthogonal) { :
the condition has length > 1 and only the first element will be used
Below is the code that I am using for my dataset. I also tried to copy and
paste the code from the sem.pdf file using data from CNES but I still get
an error message.
Any help would be much appreciated.
germany2009 <- read.dta("StataforR.dta")
#attach(germany2009)
viewData(germany2009)
#
hcor <- function(data) hetcor(data, std.err=F, pd=T)$correlations
hetcor=hcor(germany2009)
hetcor
cdupid MERKELLIKE CDULIKE MERKELPROX CDUPROX cdupid 1.0000000 0.5073577 0.6070514 -0.4064925 -0.4146810 MERKELLIKE 0.5073577 1.0000000 0.7832340 -0.4405571 -0.4200360 CDULIKE 0.6070514 0.7832340 1.0000000 -0.4868895 -0.5042006 MERKELPROX -0.4064925 -0.4405571 -0.4868895 1.0000000 0.8789219 CDUPROX -0.4146810 -0.4200360 -0.5042006 0.8789219 1.0000000
viewData(hetcor) cdu= ' CDULIKE ~ cdupid + CDUPROX
+ MERKELLIKE ~ cdupid + MERKELPROX + CDUPROX ~ ~ cdupid + cdupid ~ ~ MERKELPROX + CDUPROX ~ ~ MERKELPROX + MERKELLIKE ~ 1 + CDULIKE ~1 '
sem.cdu= sem(cdu, hetcor, N=1861, meanstructure=F,fixed.x=F)
Warning message:
In if (orthogonal) { :
the condition has length > 1 and only the first element will be used
sem.cdu= sem(cdu, hetcor, meanstructure=F,fixed.x=F)
Warning message:
In if (orthogonal) { :
the condition has length > 1 and only the first element will be used
summary(sem.cdu)
-- Laura Schwirz PhD Candidate and IRCHSS Scholar Department of Political Science Trinity College Dublin Dublin 2 Republic of Ireland Email: schwirzl at tcd.ie [[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.
Dear Laura,
John is correct. The error is produced by the sem() function in the
lavaan package. The reason is that you did not use proper names for the
function arguments. The correct call should be:
sem.cdu= sem(cdu, sample.cov=hetcor, sampl.nobs=1861,
meanstructure=F,fixed.x=F)
But more importantly, using a polychoric correlation as input, and using
ML estimation will not produce correct standard errors and test
statistics. A better approach is to use the categorical capabilities of
lavaan and provide the full data to sem(), indicating which variables
can be considered as 'ordered'. For example:
sem.cdu= sem(cdu, data=germany2009, ordered=c("cdupid", "MERKELLIKE",
"CDULIKE", "MERKELPROX", "CDUPROX"))
Here, I assumed all variables can be considered as ordered. If some of
them should be treated as conintuous/numeric, omit them from the
ordered= argument.
By default, lavaan will use the WLSMV estimator when ordered variables
are involved.
Yves.
On 11/16/2012 10:13 PM, John Fox wrote:
Dear Laura, As I explained to you when you wrote to me directly, you're not having trouble with the polycor package, since you have AFAICS successfully computed polychoric correlation among your variables. The error is produced when you call sem(), apparently in the lavaan package (though you don't mention that), and you should address your question to the author of that package (or use an appropriate title line in your r-help posting). Best, John ------------------------------------------------ John Fox Sen. William McMaster Prof. of Social Statistics Department of Sociology McMaster University Hamilton, Ontario, Canada http://socserv.mcmaster.ca/jfox/ On Fri, 16 Nov 2012 12:58:03 +0000 Laura Maria Schwirz <schwirzl at tcd.ie> wrote:
I am currently working with R's polycor package and I have
encountered a problem. I tried to follow the steps as outlined in
the sem.pdf file where a CFA model is run using polychoric
correlations. Every time I run the command sem(model, data, N=.), I
get the following warning message: Warning message: In if
(orthogonal) { : the condition has length > 1 and only the first
element will be used
Below is the code that I am using for my dataset. I also tried to
copy and paste the code from the sem.pdf file using data from CNES
but I still get an error message. Any help would be much
appreciated.
germany2009 <- read.dta("StataforR.dta") #attach(germany2009)
viewData(germany2009) # hcor <- function(data) hetcor(data,
std.err=F, pd=T)$correlations hetcor=hcor(germany2009) hetcor
cdupid MERKELLIKE CDULIKE MERKELPROX CDUPROX cdupid 1.0000000 0.5073577 0.6070514 -0.4064925 -0.4146810 MERKELLIKE 0.5073577 1.0000000 0.7832340 -0.4405571 -0.4200360 CDULIKE 0.6070514 0.7832340 1.0000000 -0.4868895 -0.5042006 MERKELPROX -0.4064925 -0.4405571 -0.4868895 1.0000000 0.8789219 CDUPROX -0.4146810 -0.4200360 -0.5042006 0.8789219 1.0000000
viewData(hetcor) cdu= ' CDULIKE ~ cdupid + CDUPROX
+ MERKELLIKE ~ cdupid + MERKELPROX + CDUPROX ~ ~ cdupid + cdupid ~ ~ MERKELPROX + CDUPROX ~ ~ MERKELPROX + MERKELLIKE ~ 1 + CDULIKE ~1 '
sem.cdu= sem(cdu, hetcor, N=1861, meanstructure=F,fixed.x=F)
Warning message: In if (orthogonal) { : the condition has length >
1 and only the first element will be used
1 day later
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121118/e183a706/attachment.pl>
The reason I was working with a correlation matrix is because I wanted to calculate polychoric correlations first before submitting it to the actual sem command. I was unsure whether R would use polychoric correlations when indicating which of the variables are ordered. A final question on the sample.cov=hetcor command. Does this not assume it is a sample variance-covariance rather than a correlation matrix (as calculated using the polycor package)?
Yes. Sample.cov should always be a covariance matrix, not a correlation matrix. It is pretty useless for categorical data.
I also tried using the command (thanks for the tip!)
sem.cdu= sem(cdu, data=germany2009, ordered=c("cdupid", "MERKELLIKE",
"CDULIKE", "MERKELPROX", "CDUPROX"))
but I always get the error result:
semcdu=sem(cdu, data=germany2009, ordered=c("cdupid", "MERKELLIKE",
"CDULIKE", "MERKELPROX", "CDUPROX"), fixed.x=F)
Error in tmp[cbind(REP$row[idx], REP$col[idx])] <- partable$free[idx] :
NAs are not allowed in subscripted assignments
It looks like you have some issues with your model specification. Could you remove all '~~' statements and try again? If the problem remains, please send me your R script, and a snippet of the data (just enough to replicate the error). Yves.