This error occurs as a result of using a list in a model rather than a vector resulting in a type mismatch. Consequently, it is an easy error to fix because it only requires a small modification to the code rather than being a serious problem. Furthermore, you have two ways to fix this problem.

### Description of the error

The R error message, “invalid type (list) for variable” is a type mismatch error resulting from using a list instead of a vector when using the modeling function. The modeling function is designed to create a model by comparing vectors. In creating a model the vectors have to be of the same length, and the goal of the modeling is to be able to predict one value from the other.

> x = list(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

> y = as.integer(abs(rnorm(10)*10))

> m = lm(y ~ x)

Error in model.frame.default(formula = y ~ x, drop.unused.levels = TRUE) :

invalid type (list) for variable ‘x’

> summary(m)

Error in summary(m) : object ‘m’ not found

As you can see in this example of recreating the error, the x value is a list, while the y value is a vector. The problem occurs because the modeling function is looking for vectors and it does not accept lists. This error message occurs when you try to use a list in this function.

### Explanation of the error

The goal of the modeling function is to create a model of how sets of numbers relate to each other for the purpose of predicting one set of values from another. It does this by comparing each pair of numbers and then finding the mathematical model that is the closest possible fit to the pattern that they produce as a whole. Once this is carried out, it is possible to produce an approximate value of the Y value from the X value. To do this the modeling function uses vectors for this analysis, as a result, if you want to use another data type such as a list you will get an error message. The reason you get an error message is that you are using the wrong data type.

### How to fix the error

In R Programming we have two ways of fixing this error, the one that you use depends upon how much control you have over the way your data is encoded. Either way, it fixes the error by changing the data type.

> x = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

> y = as.integer(abs(rnorm(10)*10))

> m = lm(y ~ x)

> summary(m)

Call:

lm(formula = y ~ x)

Residuals:

Min 1Q Median 3Q Max

-9.709 -5.955 -1.082 6.073 11.418

Coefficients:

Estimate Std. Error t value Pr(>|t|)

(Intercept) 8.2000 5.2909 1.550 0.160

x 0.5636 0.8527 0.661 0.527

Residual standard error: 7.745 on 8 degrees of freedom

Multiple R-squared: 0.05179, Adjusted R-squared: -0.06674

F-statistic: 0.4369 on 1 and 8 DF, p-value: 0.5272

In this first fix, we simply change the list into a vector. If you are entering the data as part of the program then this is a simple process, as you can see above.

> x = list(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

> y = as.integer(abs(rnorm(10)*10))

> m = lm(y ~ unlist(x))

> summary(m)

Call:

lm(formula = y ~ unlist(x))

Residuals:

Min 1Q Median 3Q Max

-8.649 -3.436 1.042 1.595 12.436

Coefficients:

Estimate Std. Error t value Pr(>|t|)

(Intercept) 2.267 4.350 0.521 0.616

unlist(x) 1.042 0.701 1.487 0.175

Residual standard error: 6.367 on 8 degrees of freedom

Multiple R-squared: 0.2165, Adjusted R-squared: 0.1186

F-statistic: 2.211 on 1 and 8 DF, p-value: 0.1753

The second fix uses the unlist function to convert the list into a vector. This procedurally changes the data type to a vector. It comes in handy when you are loading data from an outside source from that of the program.

This is a fairly easy error to deal with, not only do you have two ways of fixing this error, but both fixes are quite easy. They both involve relatively minor changes to the code so as to change a list to a vector. The degree of control that you have over the data will determine which one of these methods you use.