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, 2022 in Data Science by Avinash
• 240 points
578 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.

Discover the world of data with our Data Science Course and embark on a transformative journey towards analytical excellence.

answered Jun 1, 2022 by Sohail
• 3,040 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, 2022 in Data Science by avinash
• 1,840 points
573 views
0 votes
0 answers

How does cut with breaks work in R

I've tried using?cut but haven't been able ...READ MORE

Jul 6, 2022 in Data Science by avinash
• 1,840 points
547 views
0 votes
0 answers

How does cut with breaks work in R

I've tried using?cut but haven't been able ...READ MORE

Jul 9, 2022 in Data Science by avinash
• 1,840 points
412 views
0 votes
0 answers

How to deal with NaN values in R?

Due of my inexperience with programming and ...READ MORE

Jul 9, 2022 in Data Science by avinash
• 1,840 points
356 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,750 points
1,229 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,700 points
906 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, 2022 in Data Science by Sohail
• 3,040 points
1,064 views
0 votes
1 answer

Problem with sample function in R

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

answered Jun 24, 2022 in Data Science by Sohail
• 3,040 points
503 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP