所以我有这个数据 -
hours A B id
2.9 1 1 1
2.3 1 1 2
2.3 1 1 3
2.5 1 1 4
4.6 1 2 1
attach(hayfever.data)
hayfever.lm = lm(hayfever.data$hours ~ hayfever.data$A * hayfever$B)
但是我收到了这个错误 -
Error: invalid type (NULL) for variable 'hayfever.data$A'
当我这样做时,我也是这样 -
hayfever.lm = lm(hayfever.data$hours ~ A * B)
除变量名称更改外。
为什么会这样?
输出输出
## structure(list(hours = c(2.9, 2.3, 2.3, 2.5, 4.6, 4.4, 4.9, 4.5,
## 4.8, 4.5, 4.4, 4.6, 5.8, 5.2, 5.2, 5.3, 8.4, 9.1, 8.7, 9, 9.1,
## 9.4, 8.7, 9.4, 6.4, 5.7, 5.9, 7.2, 9.9, 10.5, 10.6, 12.1, 13.5,
## 13, 13.3, 16.2), A = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
## 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L,
## 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), B = c(1L, 1L, 1L, 1L,
## 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
## 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L
## ), id = c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L,
## 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L,
## 2L, 3L, 4L, 1L, 2L, 3L, 4L)), .Names = c("hours", "A", "B", "id"
## ), class = "data.frame", row.names = c(NA, -36L))
答案 0 :(得分:1)
此处无需使用attach
。你应该怎么做:
lm(hours ~ A * B,data=hayfever.data)
Call:
lm(formula = hours ~ A * B, data = hayfever.data)
Coefficients:
(Intercept) A B A:B
0.4 NA 2.1 NA
数据在哪里:
hayfever.data <- read.table(text='hours A B id
+ 2.9 1 1 1
+ 2.3 1 1 2
+ 2.3 1 1 3
+ 2.5 1 1 4
+ 4.6 1 2 1',header=TRUE)
答案 1 :(得分:1)
如果我使用dput
的输出将数据输入到R中,则代码可以正常工作。
hayfever.data <- structure(list(hours = c(2.9, 2.3, 2.3, 2.5, 4.6, 4.4, 4.9, 4.5,
4.8, 4.5, 4.4, 4.6, 5.8, 5.2, 5.2, 5.3, 8.4, 9.1, 8.7, 9, 9.1,
9.4, 8.7, 9.4, 6.4, 5.7, 5.9, 7.2, 9.9, 10.5, 10.6, 12.1, 13.5,
13, 13.3, 16.2), A = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), B = c(1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L
), id = c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L,
2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L,
2L, 3L, 4L, 1L, 2L, 3L, 4L)), .Names = c("hours", "A", "B", "id"
), class = "data.frame", row.names = c(NA, -36L))
hayfever.lm <- lm(hours ~ A * B, data=hayfever.data)
hayfever.lm
# Call:
# lm(formula = hours ~ A * B, data = hayfever.data)
#
# Coefficients:
# (Intercept) A B A:B
# 1.9833 0.4208 -0.5542 1.4062
你的R会话中可能存在一些挥之不去的问题,可能来自attach
? attach
是邪恶的。不要使用它。说真的,这会引起很多麻烦。
尝试按照评论中的一些建议并清除工作区,然后重新启动R会话。请勿使用attach
。
奇怪的是,您附加的屏幕截图与我从dput
输出中读取的数据不符。
答案 2 :(得分:1)
我在瑞士数据集中遇到了同样的错误。结果我的问题是因为区分大小写。 D'哦!基本上,R告诉我们引用的变量不存在,所以我们可能更好地查看我们的命名/拼写解决方案。我正在使用微软的R发行版,所以也许其他人不那么挑剔?
> lm(swiss$catholic~swiss$fertility, swiss)
Error in model.frame.default(formula = swiss$catholic ~ swiss$fertility, :
invalid type (NULL) for variable 'swiss$catholic'
> lm(swiss$Catholic~swiss$Fertility)
Call:
lm(formula = swiss$Catholic ~ swiss$Fertility, swiss)
Coefficients:
(Intercept) swiss$Fertility
-67.441 1.548
>