ethane
ethane

Reputation: 319

Linear Regression in R: "Error in eval(expr, envir, enclos) : object not found"

I'm trying to do a simple least-squares regression in R and have been getting errors constantly. This is really frustrating, can anyone point out what I am doing wrong?

First I attach the dataset (17 variables, 440 observations, each observation on a single line, no column titles). Here, I get a "masked" error. From what I've read, the "masked" error happens when objects overlap. However here I am not using any packages but the default, and I loaded a new workspace image before this. Not sure what this error refers to?

> cdi=read.table("APPENC02.txt", header=FALSE)
> attach(cdi)
The following objects are masked from cdi (position 3):

    V1, V10, V11, V12, V13, V14, V15, V16, V17, V2, V3, V4, V5, V6, V7, V8, V9

Next, since the data set does not come with headings, I use the colnames() command to add column names, then check my work with the head() command:

colnames(cdi)<- c("IDnmbr","Countynm","Stateabv","LandArea","totpop","youngpct","oldpct","actphy","hspbed","srscrime","hsgrad","BAgrad","povpct","unempct","pcincome","totincome","georegion")
> head(cdi)
  IDnmbr    Countynm Stateabv LandArea  totpop youngpct oldpct actphy hspbed srscrime hsgrad BAgrad povpct unempct pcincome totincome georegion
1      1 Los_Angeles       CA     4060 8863164     32.1    9.7  23677  27700   688936   70.0   22.3   11.6     8.0    20786    184230         4
2      2        Cook       IL      946 5105067     29.2   12.4  15153  21550   436936   73.4   22.8   11    etcetc(manually truncated)

Now the most annoying part: I can't get the lm() function to work!

> model1=lm(actphy~totpop)
Error in eval(expr, envir, enclos) : object 'actphy' not found

It's not a upper/lowercase issue, and i've tried "actphy" and actphy. What gives?

Also, the manual i'm following suggests using the attach() function but I've read a few posts discouraging it. What would be a better solution in this case?

Thanks!

Upvotes: 1

Views: 33487

Answers (1)

Thomas
Thomas

Reputation: 44585

As @joran comments, attach is a dangerous thing. Just see, for example, this simple set of code:

> x <- 2:1
> d <- data.frame(x=1:2, y=3:4)
> lm(y~x)
Error in eval(expr, envir, enclos) : object 'y' not found
> lm(y~x, data=d)

Call:
lm(formula = y ~ x, data = d)

Coefficients:
(Intercept)            x  
          2            1  

> attach(d)
The following object is masked _by_ .GlobalEnv:

    x

> lm(y~x, data=d)

Call:
lm(formula = y ~ x, data = d)

Coefficients:
(Intercept)            x  
          2            1  

> lm(y~x)

Call:
lm(formula = y ~ x)

Coefficients:
(Intercept)            x  
          5           -1  

Using attach puts the data.frame on the search path, which allows you to cheat in lm by not specifying a data argument. However, this means that if there are objects in your global environment that have names conflicting with objects in your data.frame, weird stuff can happen, like in the last two results in the code shown above.

Upvotes: 7

Related Questions