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


i'm trying simple least-squares regression in r , have been getting errors constantly. frustrating, can point out doing wrong?

first attach dataset (17 variables, 440 observations, each observation on single line, no column titles). here, "masked" error. i've read, "masked" error happens when objects overlap. here not using packages default, , loaded new workspace image before this. not sure error refers to?

> cdi=read.table("appenc02.txt", header=false) > attach(cdi) following objects masked cdi (position 3):      v1, v10, v11, v12, v13, v14, v15, v16, v17, v2, v3, v4, v5, v6, v7, v8, v9 

next, since data set not come headings, use colnames() command add column names, check work 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 annoying part: can't lm() function work!

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

it's not upper/lowercase issue, , i've tried "actphy" , actphy. gives?

also, manual i'm following suggests using attach() function i've read few posts discouraging it. better solution in case?

thanks!

as @joran comments, attach dangerous thing. see, example, 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) following object 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 data.frame on search path, allows cheat in lm not specifying data argument. however, means if there objects in global environment have names conflicting objects in data.frame, weird stuff can happen, in last 2 results in code shown above.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -