R中的线性回归:“eval(expr,envir,enclos)中的错误:找不到对象" [英] Linear Regression in R: "Error in eval(expr, envir, enclos) : object not found"
问题描述
我正在尝试在 R 中进行简单的最小二乘回归,但一直出现错误.这真的很令人沮丧,谁能指出我做错了什么?
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?
首先我附上数据集(17 个变量,440 个观察值,每个观察值在一行上,没有列标题).在这里,我收到一个掩码"错误.从我读过的内容来看,当对象重叠时会发生屏蔽"错误.但是在这里我没有使用任何包而是默认的,并且在此之前我加载了一个新的工作区图像.不确定这个错误指的是什么?
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
接下来,由于数据集没有标题,我使用colnames()
命令添加列名,然后用head()
检查我的工作命令:
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)
现在最烦人的部分:我无法让 lm() 函数工作!
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
这不是大写/小写的问题,我试过 "actphy"
和 actphy
.什么给?
It's not a upper/lowercase issue, and i've tried "actphy"
and actphy
. What gives?
此外,我遵循的手册建议使用 attach()
函数,但我读过一些不鼓励它的帖子.在这种情况下,什么是更好的解决方案?
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?
谢谢!
推荐答案
正如@joran 评论的那样,attach
是一件危险的事情.看看,例如,这组简单的代码:
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
使用 attach
将 data.frame 放在搜索路径上,这允许您通过不指定 data
参数来欺骗 lm
.但是,这意味着如果您的全局环境中存在名称与 data.frame 中的对象发生冲突的对象,则可能会发生奇怪的事情,例如上面代码中的最后两个结果.
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.
这篇关于R中的线性回归:“eval(expr,envir,enclos)中的错误:找不到对象"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!