[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
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:


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) &
#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