使用来自fivethirtyeight包的数据......
library(fivethirtyeight)
grads <- college_recent_grads
创建了梯度数据的子集以包含所需的变量
data <- grads[, c("men", "major_category", "employed",
"employed_fulltime_yearround", "p25th",
"p75th", "total")]
然后,我将data
子集按主要类别划分,并省略了数据中的一个NA值
majorcats <- split(data, data$major_category)
names(majorcats)
majorcats <- majorcats %>% na.omit()
并尝试在名为facts的函数中运行回归模型,其中用户可以指定x,y和z,z是主要类别(因此我将major_category拆分为data
子集)< / p>
facts <- function(x, y, z){
category <- majorcats[["z"]]
summary(lm(y ~ x, data = category))
}
不幸的是,当我尝试将变量输入事实时(这是majorcats数据集的一部分,例如
facts(men, p25th, Arts)
我收到以下错误:
Error in model.frame.default(formula = y ~ x, data = category,
drop.unused.levels = TRUE) :
invalid type (NULL) for variable 'y'
Called from: model.frame.default(formula = y ~ x, data = category,
drop.unused.levels = TRUE)
Browse[1]>
有人可以解释一下这个错误意味着什么,以及我怎么能解决它?
答案 0 :(得分:1)
只需将参数作为字符串文字传递,然后从字符串中创建公式:
facts <- function(x, y, z){
category <- majorcats[[z]]
model <- as.formula(paste(y, "~", x))
# ALTERNATIVE: model <- reformulate(x, response=y)
summary(lm(model, data = category))
}
facts("men", "p25th", "Arts")