将变量的字符向量传递给 selection() 公式

Passing a character vector of variables into selection() formula

当我通过 lm() 函数计算 R 中的线性模型时,可以将变量的字符向量传递到 lm() 公式中。 (例如,如 here or here 所述。)但是,如果我将相同的方法应用于 sampleSelection 包的 selection() 函数,则会出现以下错误:

Error in detectModelType(selection, outcome) : argument 'selection' must be a formula in function 'selection()'

问题:有没有办法将变量的字符向量传递给selection()公式?

您可以在下面找到一个可重现的示例,它说明了问题:

# Example data
N <- 1000
y <- rnorm(N, 2000, 200)
y_prob <- c(rep(0, N / 2), rep(1, N / 2)) == 1
x1 <- y + rnorm(N, 0, 300)
x2 <- y + rnorm(N, 0, 300)
x3 <- y + rnorm(N, 0, 300)
x4 <- y + rnorm(N, 0, 300)
x5 <- y + rnorm(N, 0, 300)
y[1:(N / 2)] <- 0
data <- data.frame(y, x1, x2, x3, x4, x5, y_prob)
x_vars <- colnames(data)[colnames(data) %in% c("y", "y_prob") == FALSE]

# Estimate linear model via lm() --> works without any problems
lm(paste("y", "~", paste(x_vars, collapse = " + ")))

# Estimate Heckman model via selection()
library("sampleSelection")

# Passing of vector does not work
selection(paste("y_prob", "~", paste(x_vars[1:4], collapse = " + ")), 
      paste("y", "~", paste(x_vars[3:5], collapse = " + ")), data)

# Formula has to be written manually
selection(y_prob ~ x1 + x2 + x3 + x4, y ~ x3 + x4 + x5, data)

as.formula

结束你的 paste 通话
selection(as.formula(paste("y_prob", "~", paste(x_vars[1:4], collapse = " + "))), 
  as.formula(paste("y", "~", paste(x_vars[3:5], collapse = " + "))), data)


Call:
 selection(selection = as.formula(paste("y_prob", "~", paste(x_vars[1:4],      collapse = " + "))), outcome = as.formula(paste("y", "~",      paste(x_vars[3:5], collapse = " + "))), data = data) 

Coefficients:
S:(Intercept)           S:x1           S:x2           S:x3           S:x4  O:(Intercept)           O:x3           O:x4           O:x5          sigma  
   -1.936e-01     -5.851e-05      7.020e-05      5.475e-05      2.811e-05      2.905e+02      2.286e-01      2.437e-01      2.165e-01      4.083e+02  
      rho  
1.000e+00