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?
我试着在R中做一个简单的最小二乘回归,并且不断地出错。这真是令人沮丧,有人能指出我做错了什么吗?
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?
首先,我附加了数据集(17个变量,440个观察值,每个观察在一行,没有列标题)。在这里,我得到一个“蒙面”错误。从我所读到的内容来看,当对象重叠时,“掩码”错误就会发生。但是,这里我没有使用任何包,而是使用默认值,在此之前我加载了一个新的工作区映像。不知道这个错误指的是什么?
> 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()命令来添加列名,然后用head()命令检查我的工作:
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!
现在最烦人的部分是:我无法获得lm()函数来工作!
> 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?
这不是一个大写/小写的问题,我试过“actphy”和actphy。到底发生了什么事?
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?
另外,我所遵循的手册建议使用attach()函数,但我读过一些文章使它令人沮丧。在这种情况下,什么是更好的解决方案?
Thanks!
谢谢!
1 个解决方案
#1
7
As @joran comments, attach
is a dangerous thing. Just see, for example, this simple set of code:
正如@joran所说,附加是一件危险的事情。看看,例如,这个简单的代码集:
> 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.
使用attach将data.frame放在搜索路径上,这允许您在lm中进行欺骗,而不是指定数据参数。但是,这意味着如果在您的全局环境中存在与您的数据中对象相冲突的名称的对象,那么会发生一些奇怪的事情,比如上面显示的代码中的最后两个结果。
#1
7
As @joran comments, attach
is a dangerous thing. Just see, for example, this simple set of code:
正如@joran所说,附加是一件危险的事情。看看,例如,这个简单的代码集:
> 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.
使用attach将data.frame放在搜索路径上,这允许您在lm中进行欺骗,而不是指定数据参数。但是,这意味着如果在您的全局环境中存在与您的数据中对象相冲突的名称的对象,那么会发生一些奇怪的事情,比如上面显示的代码中的最后两个结果。