简体   繁体   中英

Pass dynamically variable names in lm formula inside a function

I have a function that asks for two parameters:

  • dataRead (dataframe from the user)
  • variableChosen (which dependent variable the user wants to utilize in the model)

Obs: indepent variable will always be the first column

But if the user gives me for example, a dataframe called dataGiven which columns names are: "Doses", "Weight" I want that my model name has these names in my results

My actual function correctly make the lm, but my formula names from the data frame are gone (and shows how I got the data from the function)

    Results_REG<- function (dataRead, variableChosen){

      fit1 <- lm(formula = dataRead[,1]~dataRead[,variableChosen])

      return(fit1)
      }

When I call:

    test1 <- Results_REG(dataGive, "Weight")
    names(teste1$model)

shows:

    "dataRead[, 1]"            "dataRead[, variableChosen]"

I wanted to show my dataframe columns names, like:

    "Doses"            "Weight"

First off, it's always difficult to help without a reproducible code example . For future posts I recommend familiarising yourself with how to provide such a minimal reproducible example.

I'm not entirely clear on what you're asking, so I assume this is about how to create a function that fits a simple linear model based on data with a single user-chosen predictor var .

Here is an example based on mtcars

results_LM <- function(data, var) {
    lm(data[, 1] ~ data[, var])
}

results_LM(mtcars, "disp")
#Call:
#lm(formula = data[, 1] ~ data[, var])
#
#Coefficients:
#(Intercept)  data[, var]
#   29.59985     -0.04122

You can confirm that this gives the same result as

lm(mpg ~ disp, data = mtcars)

Or perhaps you're asking how to carry through the column names for the predictor? In that case we can use as.formula to construct a formula that we use together with the data argument in lm .

results_LM <- function(data, var) {
    fm <- as.formula(paste(colnames(data)[1], "~", var))
    lm(fm, data = data)
}

fit <- results_LM(mtcars, "disp")
fit
#Call:
#lm(formula = fm, data = data)
#
#Coefficients:
#(Intercept)         disp
#   29.59985     -0.04122

names(fit$model)
#[1] "mpg"  "disp"
outcome <- 'mpg'
model <- lm(mtcars[,outcome] ~ . ,mtcars) 

yields the same result as:

data(mtcars)
model <- lm( mpg ~ . ,mtcars)

but allows you to pass a variable (the column name). However, this may cause an error where mpg is included in the right hand side of the equation as well. Not sure if anyone knows how to fix that.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM