[R] Error in model.frame.default… variable lengths differ (Without NA's)
Jorge Cimentada
cimentadaj at gmail.com
Wed Dec 2 17:35:21 CET 2015
Hello everyone,
I'm running an ordinal logistic and I keep getting this error:
Error in model.frame.default(formula = eduattain ~ dadedu, data =
workdataset, :
variable lengths differ (found for '(weights)')
I looked at several similar questions on the internet and ended up deleting
all the NA's from all the variables in the model prior to running the
model. I checked this before(where the length differed) and after deleting
the NA's(where the length was the same)
A similar question
<http://t.sidekickopen29.com/e1t/c/5/f18dQhb0S7lC8dDMPbW2n0x6l2B9nMJN7t5XX4RYygTW5v0Dpn5wvpr8W4X9Hq256dwC8d2Ntnd02?t=http%3A%2F%2Fstackoverflow.com%2Fquestions%2F29220727%2Ferror-in-model-frame-default-variable-lengths-differ-but-no-nas-in-data&si=4832790679388160&pi=5c2682a6-6cfb-4c8b-98c9-ca8956c3fe4c>
posed
a similar problem(same error, no NA's) but no answer was given due to the
problem not being reproducible.
------------------------------
Here is a a reproduction of the problem: here I recoded empty rows into
NA's thinking that empty rows might have something to do with this:
require(MASS)
spain <- read.csv("http://vs-web-fs-1.oecd.org/piaac/puf-data/CSV/Prgespp1.csv")[,c("B_Q01a","J_Q07b","SPFWT0")]
workdataset <- spain
workdataset$eduattain <- workdataset$B_Q01a
workdataset$dadedu <- workdataset$J_Q07b
# Model
ordinalmodel <- polr(eduattain ~ dadedu, data = workdataset, weights =
"SPFWT0", Hess = TRUE)
## Recoding some empty rows into NA's
workdataset$eduattain[workdataset$B_Q01a == ""] <- NA
workdataset$dadedu[workdataset$J_Q07b == ""] <- NA
workdataset <- workdataset[!is.na(workdataset$dadedu) &
!is.na(workdataset$eduattain),]
length(workdataset$SPFWT0[!is.na(workdataset$eduattain)])
length(workdataset$SPFWT0[!is.na(workdataset$dadedu)])
#Problem persists
ordinalmodel <- polr(eduattain ~ dadedu, data = workdataset, weights =
"SPFWT0", Hess = TRUE)
*Note: This study has over 10 datasets for different countries and
regardless of the dataset I use the problem persists.*
I've tried all sorts of different things to fix this(I thought it was the
way I recoded the dependent and independent variables, I thought it was
because of empty rows). If I exclude the weight variable, the model works
just fine. I tried looking at the length of the weight when the two
different variables are not NA's and the length is the same.
Please, any help is appreciated.
*Jorge C.*
[[alternative HTML version deleted]]
More information about the R-help
mailing list