简体   繁体   English

如何执行线性回归而没有错误?

[英]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.

相关问题 如何在数据框中的所有连续变量之间执行和存储线性回归模型? - How can I perform and store linear regression models between all continuous variables in a data frame? 如何为这个线性 model 构建回归? - How can I build a regression for this linear model? 如何对我的数据执行非线性回归 - How to perform a non linear regression for my data 我打算通过for循环附加我的线性回归结果,但出现错误。 我该如何解决? - I've intended attach my linear regression result by for loop but got an error. How can I solve it? 如何将因子更改为数字变量或以其他方式处理我在线性回归中遇到的这个错误 - How can I change the factors to numeric variables or otherwise deal with this error I'm getting in my linear regression 如何基于另一个线性回归从线性回归中过滤出行 - How can I filter out rows from linear regression based on another linear regression 如何使用R在散点图上创建线性回归线? - How can I create a linear regression line on a scatterplot with R? 如何从线性回归分析中计算斜率? - How can I calculate the slope from a linear regression analysis? 如何对这些数据执行简单的线性回归模型? - How can I peform a simple linear regression model on this data? 如何从R中的线性回归预测单个值? - How can I predict a single value from a linear regression in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM