user3302483
user3302483

Reputation: 855

Termplot regression plot not working

I'm trying to create a termplot in R but I'm getting an error message around undefined columns. I've seen this example work on a video so not sure if something has changed between R versions?

data(airquality) #load airquality dataset
model1 <- lm(airquality$Ozone ~ airquality$Solar.R)
termplot(model1)

Error in `[.data.frame`(xx, use.rows) : undefined columns selected

Upvotes: 1

Views: 754

Answers (1)

eipi10
eipi10

Reputation: 93811

Specify the data frame using the data argument to lm and termplot will work:

model2 <- lm(Ozone ~ Solar.R, data=airquality)
termplot(model2)

The error is saying that termplot is trying to plot one or more columns that don't exist in the data. If you look at each version of the model (see below), you can see what's going wrong: In model1, the coefficient name is not the same as the column name in the data frame (because it includes the data frame name), but in model2 the coefficient name corresponds to the column name in the data frame.

> model1

Call:
lm(formula = airquality$Ozone ~ airquality$Solar.R)

Coefficients:
       (Intercept)  airquality$Solar.R  
           18.5987              0.1272  

> model2

Call:
lm(formula = Ozone ~ Solar.R, data = airquality)

Coefficients:
(Intercept)      Solar.R  
    18.5987       0.1272  

Upvotes: 2

Related Questions