1

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"
Artur Guerra
  • 75
  • 2
  • 8

2 Answers2

10

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"
Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
  • 1
    Sorry for the missing example, next will remember it! And the second one It is exactly what I wanted, worked fine! – Artur Guerra Apr 27 '19 at 06:21
0
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.

Mark
  • 389
  • 1
  • 7
  • 19