I was trying to shorten the process of passing the formula to the regsubsets() function instead of having to write the full string. So, I used the following code for generation of formula string but the regsubsets function gave the error "argument "y" is missing".
When I pasted the generated formula string without quotes, it was accepted. But when I pasted formula string within quotes, the same error was generated. So, it seems to me that, the quotes are the problem.
How can I bypass this error?
Is there another function that can pass a string argument without quotes to such picky functions?
Here's the code sample:
# load data
data(mtcars) # pre-loaded in R
head(mtcars,2)
# mpg cyl disp hp drat wt qsec vs am gear carb
# Mazda RX4 21 6 160 110 3.9 2.620 16.46 0 1 4 4
# Mazda RX4 Wag 21 6 160 110 3.9 2.875 17.02 0 1 4 4
## full model formula
# response & predictors
yvar = c('mpg')
xvars = setdiff(colnames(mtcars), yvar)
xvars
# 'cyl''disp''hp''drat''wt''qsec''vs''am''gear''carb'
# formula string
fstr = paste(yvar, '~', paste(setdiff(names(mtcars), yvar), collapse='+'))
fstr
# 'mpg ~ cyl+disp+hp+drat+wt+qsec+vs+am+gear+carb'
## regsubsets regression
library(leaps)
subset = regsubsets(fstr, data=mtcars, method='exhaustive', nbest=2)
res = summary(subset)
res
# Error in leaps.setup(x, y, wt = weights, nbest = nbest, nvmax = nvmax, : argument "y" is
# missing, with no default
# Traceback:
# 1. regsubsets(fstr, data = mtcars, method = "exhaustive", nbest = 2)
# 2. regsubsets.default(fstr, data = mtcars, method = "exhaustive", nbest = 2)
# 3. leaps.setup(x, y, wt = weights, nbest = nbest, nvmax = nvmax,
# . force.in = force.in, force.out = force.out, intercept = intercept)
You're on the right track about the quotes. These indicate that you are passing a character string ( fstr
) rather than a formula to regsubsets
. But a character string is not a formula, even if it is a character string of a formula. Turning a character string of a formula into an actual formula is as easy as as.formula(fstr)
.
So that minor modification produces this result:
library(leaps)
data(mtcars)
yvar <- 'mpg'
xvars <- setdiff(colnames(mtcars), yvar)
fstr <- paste(yvar, '~', paste(setdiff(names(mtcars), yvar), collapse = '+'))
fstr <- as.formula(fstr)
subset = regsubsets(fstr, data = mtcars, method = 'exhaustive', nbest = 2)
res = summary(subset)
res
#> Subset selection object
#> Call: regsubsets.formula(fstr, data = mtcars, method = "exhaustive",
#> nbest = 2)
#> 10 Variables (and intercept)
#> Forced in Forced out
#> cyl FALSE FALSE
#> disp FALSE FALSE
#> hp FALSE FALSE
#> drat FALSE FALSE
#> wt FALSE FALSE
#> qsec FALSE FALSE
#> vs FALSE FALSE
#> am FALSE FALSE
#> gear FALSE FALSE
#> carb FALSE FALSE
#> 2 subsets of each size up to 8
#> Selection Algorithm: exhaustive
#> cyl disp hp drat wt qsec vs am gear carb
#> 1 ( 1 ) " " " " " " " " "*" " " " " " " " " " "
#> 1 ( 2 ) "*" " " " " " " " " " " " " " " " " " "
#> 2 ( 1 ) "*" " " " " " " "*" " " " " " " " " " "
#> 2 ( 2 ) " " " " "*" " " "*" " " " " " " " " " "
#> 3 ( 1 ) " " " " " " " " "*" "*" " " "*" " " " "
#> 3 ( 2 ) "*" " " "*" " " "*" " " " " " " " " " "
#> 4 ( 1 ) " " " " "*" " " "*" "*" " " "*" " " " "
#> 4 ( 2 ) " " " " " " " " "*" "*" " " "*" " " "*"
#> 5 ( 1 ) " " "*" "*" " " "*" "*" " " "*" " " " "
#> 5 ( 2 ) " " " " " " "*" "*" "*" " " "*" " " "*"
#> 6 ( 1 ) " " "*" "*" "*" "*" "*" " " "*" " " " "
#> 6 ( 2 ) " " "*" "*" " " "*" "*" " " "*" "*" " "
#> 7 ( 1 ) " " "*" "*" "*" "*" "*" " " "*" "*" " "
#> 7 ( 2 ) "*" "*" "*" "*" "*" "*" " " "*" " " " "
#> 8 ( 1 ) " " "*" "*" "*" "*" "*" " " "*" "*" "*"
#> 8 ( 2 ) " " "*" "*" "*" "*" "*" "*" "*" "*" " "
Created on 2020-12-18 by the reprex package (v0.3.0)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.