我正在尝试进行OLS回归,并且不断收到错误消息,提示找不到某个变量。我是R的新手。
除最后一行外,所有代码均有效。
load("psub.Rdata")
VarsForOLS.tbl <- psub %>%
mutate(personalIncome = PINCP, groupingID = ORIGRANDGROUP, age = AGEP, sex = SEX, workingclass = COW, educationalLevel = SCHL) %>%
select(personalIncome, groupingID, age, sex, workingclass, educationalLevel)
trainingIncome.data <- subset(VarsForOLS.tbl, groupingID >=500)
testingIncome.data <- subset(VarsForOLS.tbl, groupingID < 500)
y <- "log(personalIncome, base=10)"
explanatoryVariables <- c("age", "sex", "workingclass", "educationLevel")
olsModel <- paste(y, paste(explanatoryVariables, collapse = "+"), sep = "-")
trainingIncome.ols <- lm(olsModel, data = trainingIncome.data)
我希望能够进行线性回归,但错误提示:
Error in eval(parse(text = x, keep.source = FALSE)[[1L]]) :
object 'personalIncome' not found
答案 0 :(得分:1)
为获得最佳帮助,您应该发布reproducible example。
您正在使用-
生成公式,该~
应该是olsModel <- reformulate(explanatoryVariables, response="y")
。更好的是,@ benbolker建议使用此便捷功能
y
它将自动解析字符向量并添加print(olsModel)
变量作为响应,因此您不必担心波浪号和粘贴等问题。
通常,如果您对这些事情不满意,我建议您在不进行所有参数化的情况下尝试模型(只需输入即可!),然后看看它是否首先运行。另外,尝试DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(name VARCHAR(12) NOT NULL
,food VARCHAR(12) NOT NULL
,PRIMARY KEY(name,food)
);
INSERT INTO my_table VALUES
('Lisa','Grapes'),
('Victoria','Broccoli'),
('Oliver','Carrot'),
('Lisa','Apple'),
('Kayla','Pineapple'),
('Oliver','Banana'),
('Victoria','Kiwi'),
('Kayla','Grapes');
SELECT DISTINCT x.name
FROM my_table x
LEFT
JOIN my_table y
ON y.name = x.name
AND y.food = 'grapes'
WHERE x.food <> 'grapes'
AND y.name IS NULL;
+----------+
| name |
+----------+
| Oliver |
| Victoria |
+----------+
查看最终粘贴在一起的内容。