Creating new Functions with Linear Regression in R

0 votes

Having problem 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)

Jun 1 in Data Science by Avinash
• 240 points
22 views

1 answer to this question.

0 votes

When we want to create a model with an arbitrary number of independent variables, we 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 example, 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.

answered Jun 1 by Sohail
• 2,280 points

Related Questions In Data Science

0 votes
0 answers

Introduction to Statistical Learning with Applications in R Figure Codes

I recently bought the following book: An Introduction ...READ MORE

Jun 1 in Data Science by avinash
• 860 points
19 views
0 votes
1 answer

Difference between the == and %in% operators in R

percent in percent "returns a vector of ...READ MORE

answered Jun 20 in Data Science by Sohail
• 2,280 points
15 views
0 votes
1 answer

Difference between the == and %in% operators in R

According to help('percent in percent'), percent in ...READ MORE

answered 5 days ago in Data Science by Sohail
• 2,280 points
9 views
0 votes
1 answer

do-while loop in R

Unlike for and while loops, which test the loop condition at ...READ MORE

answered Jun 1 in Data Science by Sohail
• 2,280 points
19 views
0 votes
1 answer
0 votes
1 answer

Steps to evaluate Linear Regression in R

 These are sequential steps which need to ...READ MORE

answered Jul 25, 2018 in Data Analytics by CodingByHeart77
• 3,720 points
546 views
+5 votes
2 answers
+1 vote
1 answer

R programming: Linear programming problems

The documentation for the lp package provides ...READ MORE

answered Feb 4, 2019 in Data Analytics by Tyrion anex
• 8,690 points
346 views
0 votes
1 answer

How to make loop for one-at-a time logistic regression in R?

You're probably looking for something similar to ...READ MORE

answered Jun 20 in Data Science by Sohail
• 2,280 points
17 views
0 votes
1 answer

Problem with sample function in R

The first time works, but after that, ...READ MORE

answered 4 days ago in Data Science by Sohail
• 2,280 points
8 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP