Creating new Functions with Linear Regression in R :

拥有回忆 提交于 2020-08-26 15:17:37

问题


I'm having a trouble when creating a function that calls the lm() function:

regresionLineal <- function (vardep, varindep1, varindep2, DATA) {
  lm(vardep ~ varindep1 + varindep2, data = DATA)
  }

Then I call it using data from a data frame I created previously (DATOS)...

regresionLineal(Estatura, Largo, Ancho, DATOS)

Error in eval(expr, envir, enclos) : object 'Estatura' not found Called from: eval(expr, envir, enclos)

Any help will be welcome...


回答1:


You should do:

regresionLineal <- function (vardep, varindep1, varindep2, DATA) {
  lm(paste(vardep, "~", varindep1, "+", varindep2), data = DATA)
  }

where you pass in vardep, varindep1, varindep2 as strings. As an example, I use R's built-in trees dataset:

regresionLineal("Height", "Girth", "Volumn", trees)
# Call:
# lm(formula = paste(vardep, "~", varindep1, "+", varindep2), data = DATA)

# Coefficients:
# (Intercept)        Girth       Volume  
#     83.2958      -1.8615       0.5756  

However, I don't see why we bother doing this. If we have to specify every variable in the formula, why not simply pass in a complete formula? And in that case, you can use lm() directly without define your own function.




回答2:


Also, you may already know this, but it might be helpful to keep in mind that the regression object created here won't exist outside of the function unless assigned to the global environment or whatever environment you're working in. If you need to call the reg. object outside of this function later for some reason you should assign it as: model1 <<- lm(paste(vardep, "~", varindep1, "+", varindep2), data = DATA) to be able to call from the global env.




回答3:


If you want to create a model with an arbitrary number of independent variables, you can use the below:

create_lm <- function(data, dep, covs) {
# Create the first part of the formula with the dependent variable
  form_base <- paste(dep, "~")
# Create a string that concatenates your covs vector with a "+" between each variable
  form_vars <- paste(covs, collapse = " + ")
# Paste the two parts together
  formula <- paste(form_base, form_vars)
# Call the lm function on your formula
  lm(formula, data = data)
}

For instance, using the built-in mtcars dataset:

create_lm(mtcars, "mpg", c("wt", "cyl"))

Call:
lm(formula = formula, data = data)

Coefficients:
(Intercept)           wt          cyl  
     39.686       -3.191       -1.508  

The downside is that the printed output from the model doesn't reflect the particular call you made to lm, not sure if there is any way around this.



来源:https://stackoverflow.com/questions/38175775/creating-new-functions-with-linear-regression-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!