Form Equation in R-Multiple Regression

Asked

Viewed 80 times

1

I need to solve a question in the R and I have no idea how to make the R give me equation that adjusts the data,:

I have data from the following tables:

Idade X1 = (38,46,39,43,32)
Anos de Faculdade X2 = (4,0,5,2,4)
Rendimento em Dólares Y= (81700,73300,89500,79800,69900)

I need to create an equation in the way that adjusts this data, in the form:

y = b0 + b1x1 + b2x2

https://i.stack.Imgur.com/Amh9k.png

  • You want to do a linear regression, is that it? I didn’t edit the title to question it here in the comment.

  • Yes, I’d like to do a multiple linear regression, but I don’t know how to get R to give me the equation that fits these data.

  • Possible duplicate of Multiple Linear Regression in R. See also this question to see if it helps.

  • I took a look at the link and I believe my doubt is different. I don’t know how to ask R to give me the Regression equation

  • What do you mean "the way"? And, what do you expect to have to answer? Try to edit the question. It’s unclear.

  • I was able to do it by Excel, but I need it by R. The answer would be this: Y = -16739.60122 + 1960.924952 * X1 + 5975,657713 * X2

  • It takes the vector Y, otherwise we cannot adjust the model. The coefficients will be given by coef(lm(Y ~ X1 + X2)).

  • Is in the link, @Rui.

  • @After getting the coefficients,?

Show 4 more comments

2 answers

3

From what it seemed to me in the comments and the link you posted, your goal is to make an adjunct linear regression of a forecast. Here’s the solution:

to) analyzing

df <- data.frame(
  y = c(81.7, 73.3, 89.5, 79.8, 69.9), 
  x1 = c(38, 46, 39, 43, 32), 
  x2 = c(4, 0, 5, 2, 4)
)

reg <- lm(
  y ~ x1 + x2, data = df
)

summary(reg)

#Call:
#lm(formula = y ~ x1 + x2, data = df)

#Residuals:
#       1        2        3        4        5 
# 0.02182 -0.16295 -0.11476  0.26851 -0.01263 

#Coefficients:
#         Estimate Std. Error t value Pr(>|t|)    
#(Intercept) -16.73960    1.62926  -10.27 0.009341 ** 
#x1            1.96092    0.03529   55.56 0.000324 ***
#x2            5.97566    0.09388   63.65 0.000247 ***
#---
#Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

#Residual standard error: 0.2371 on 2 degrees of freedom
#Multiple R-squared:  0.9995,   Adjusted R-squared:  0.999 
#F-statistic:  2074 on 2 and 2 DF,  p-value: 0.0004819

b) forecasting

forec <- data.frame(x1 = 39, x2 = 4)
predict(reg, newdata = forec, interval = 'confidence', level = .95)

#       fit      lwr      upr
# 1 83.6391 83.07115 84.20706
  • 1

    Thank you very much!

  • 1

    IS x2 = 4, is not 5.

3


Simply adjust a linear model with an independent term, the intersection with the yy axis.

Y <- c(81700, 73300, 89500, 79800, 69900)
X1 = c(38,46,39,43,32)
X2 = c(4,0,5,2,4)

modelo <- lm(Y ~ X1 + X2)
coef(modelo)
#(Intercept)          X1          X2 
# -16739.601    1960.925    5975.658 

To answer the question on the link, what is the average salary of a manager of this company with X1 = 39 years of age and X2 = 4 years of college, will only be using predict.

predict(modelo, newdata = data.frame(X1 = 39, X2 = 4))
#      1 
#83639.1
  • Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.