# Error with lm in R

I'm taking a class on R and I cannot get the professors code to work. I am trying to do a simple linear model and I run this code:

fit = lm(ozone~.,data=ozone)

summary(fit)

Which keeps giving me the following error:

Error in model.frame.default(formula = ozone ~ ., data = ozone, drop.unused.levels = TRUE) : invalid type (list) for variable 'ozone

It's really depressing as they are the first two lines of code in his lecture notes. I have also found several other forum posts on this topic (it's even listed as a common r mistake), but I am too...special to figure out how to change it.

I tried reading it as.numeric, and as a data.frame, which is what most other threads suggested, but neither worked.

• The thing on the left of the ~ should be the name of the dependent variable, not the name of the data frame. Sep 25, 2013 at 8:06
• 'str(ozone)' displays the structure of the ozone data set. In particular, you can see that the variables are 'Station', 'Av8top', 'Lat', and 'Lon'. The error message says that 'lm()' does not expect 'ozone', which is none of the variables, to be the response. Sep 25, 2013 at 8:10

If we look on the first rows of the data frame ozone, we see that variable names (columns) are Station, Av8top, Lat and Lon.

> head(ozone)
Station   Av8top      Lat       Lon
1      60 7.225806 34.13583 -117.9236
2      69 5.899194 34.17611 -118.3153
3      72 4.052885 33.82361 -118.1875
4      74 7.181452 34.19944 -118.5347
5      75 6.076613 34.06694 -117.7514
6      84 3.157258 33.92917 -118.2097


When you fit model with code lm(ozone~.,data=ozone), you are telling R that there is variable ozone (column) in your data that should be dependent variable. But there are no column named ozone. So either other column should be used as dependent variable or the file you are downloading is wrong.

• It is not sufficient for the variable given in the formula to exist in the specified data set data=ozone. As the Error indicates, it also has to be of proper type, i.e. not a list.

Variable var of improper type added to the set, returns the same error:

> var = as.matrix(as.list(1:dim(ozone)[1])
> ozone=cbind(ozone,var)
> lm(var ~ .,data=ozone)

• It is not necessary that the variable (of the proper type) specified in the formula exists in the given data set. Instead, it can just exist in your current environment.

Variable var of proper type, but outside the set, can be used without error:

> var = ozone\$Station
> lm(var ~ .,data=ozone)


Notice: If the variable var specified in the formula does not exist in your current environment, the following error is returned: