Today I re-run some code in R and was greeted with an error “Error in factorize(formula, data, f_out = TRUE)” and (more specifically) “Unable to find variable to convert to factor.” I immediately suspected the as.factor(x)
among the predictor variables to be the culprit, but since this is analysis that has worked a few days earlier (on a different machine), I quickly searched on the web and found nothing. For some reason, in this case the as.factor(x)
did not work, and the solution was simple: create a new (factor) variable separately and then run the regression analysis with the new variable. So instead of:
z <- zelig(y ~ x1 + as.factor(x2) + x3, model="normal", data=d)
I first create the new variable:
d$x2_factor <- as.factor(d$x2)
and then run the regression analysis with the new variable:
z <- zelig(y ~ x1 + x2_factor + x3, model="normal", data=d)
I just thought I’d share this in case someone else comes across this error and doesn’t find it obvious what the solution is.