Solved – Error with lm in R

linear modellmr

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:

ozone<-read.table("http://www.ats.ucla.edu/stat/r/faq/ozone.csv", sep=",", header=T)

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.

Best Answer

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.

Related Question