Error in 1 | country : operations are possible only for numeric, logical or complex types

I have imputed missing data using mice, and run the first multilevel model (empty model):

tom <- with(imputer, lm(attitude ~ 1 + (1 | country)))

But, I get the following error:

Error in 1 | country : operations are possible only for numeric, logical or complex types

Country is a character variable. I have tried to set it as.factor, but this creates another type of error.. What can the problem be?

Hi,

Welcome to the RStudio community!

The problem is that a linear regression model can only work with numeric data and not categorical (it can with logical i.e. 0 - 1). A way around it so convert a categorical variable into a one-hot representation, i.e. one column per value with 0 or 1. Normally the lm() function should be able to do this automatically like so:

lm(Sepal.Length ~ Species + Petal.Width, data = iris)
#> 
#> Call:
#> lm(formula = Sepal.Length ~ Species + Petal.Width, data = iris)
#> 
#> Coefficients:
#>       (Intercept)  Speciesversicolor   Speciesvirginica        Petal.Width  
#>           4.78044           -0.06025           -0.05009            0.91690

Created on 2021-07-09 by the reprex package (v2.0.0)
You can see that the Species variable gets split into n - 1 new inputs

There seems to be an issue with your data preventing this, but it's hard to say without being able to run the code. So I suggest you create a reprex and share that so we can figure this out. A reprex consists of the minimal code and data needed to recreate the issue/question you're having. You can find instructions how to build and share one here:

Hope this helps,
PJ

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.