[英]How can I perform Linear regression without error?
Im trying to do an OLS regression and I keep getting an error message that a certain variable cannot be found. 我正在尝试进行OLS回归,但不断收到错误消息,指出找不到某个变量。 I am a newbie to R.
我是R的新手。
All the code works except for the last line. 除最后一行外,所有代码均有效。
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)
I expect to be able to run the linear regression but the error says: 我希望能够运行线性回归,但错误提示:
Error in eval(parse(text = x, keep.source = FALSE)[[1L]]) :
object 'personalIncome' not found
For the best help you should post a reproducible example . 为了获得最佳帮助,您应该发布一个可复制的示例 。
You are generating your formula with a -
which should be a ~
. 您正在使用
-
生成公式,该公式应为~
。 Even better, @benbolker suggested this handy function 更好的是,@ benbolker建议使用此方便的功能
olsModel <- reformulate(explanatoryVariables, response="y")
which will automatically parse the character vector and add the y
variable as response, so you don't have to worry about tildes and paste and so on. 它将自动解析字符向量并添加
y
变量作为响应,因此您不必担心波浪号和粘贴等问题。
Generally, if you're stuck on these kinds of things I'd recommend trying the model without all the parameterisation (just type it out!) and seeing if that runs first. 通常,如果您对这些事情不满意,我建议您在不进行所有参数化的情况下尝试模型(只需将其键入!),然后看看它是否首先运行。 Also, try
print(olsModel)
to see what you've ended up pasting together. 另外,尝试使用
print(olsModel)
查看最终粘贴在一起的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.