Home » excel » excel – LINEST function analogue for R

excel – LINEST function analogue for R

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am new to R, and trying to use it. I need to get LINEST function equivalent for R.

I can get lots of URL for least square from Google but I am not able to get one with constant values. I would like to use it for ANOVA.

For example, I want to use those arrays. first is y, and the others are dummy variables.

y = c(42,37,41,39,43,41,37,40,39,38,41,32,28,34,32,30,33)

x1 = c(1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0)

x2 = c(0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0)

x3 = c(0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1)

If case of, LINEST, I can get the result by using LINEST(Y1:YN,X11:X3N,TRUE).

P.S. in this case I should have the result as below:

In this case, I should have the result as below:

| B | C | const |

| -9 | -1 | 40.5 |

| 1.16 | 1.22 | 0.82 |

| 0.83 | 2.01 | |

| 33.74 | 14 |

| 274.76 | 57 |

How to&Answers:

I believe you are looking for summary(lm(y~x1+x2+x3)), which should be easy to find out with Google if one knows that this is called “linear regression”. However, you wouldn’t even need to do the dummy encoding manually. Just use a factor variable and let R do it for you.

summary(lm(y~x1+x2+x3))

#Call:
#lm(formula = y ~ x1 + x2 + x3)
#
#Residuals:
#   Min     1Q Median     3Q    Max 
#  -3.5   -1.5    0.5    1.5    2.5 
#
#Coefficients: (1 not defined because of singularities)
#            Estimate Std. Error t value Pr(>|t|)    
#(Intercept)  31.5000     0.8238  38.240 1.45e-15 ***
#x1            9.0000     1.1650   7.726 2.05e-06 ***
#x2            7.5000     1.2218   6.138 2.57e-05 ***
#x3                NA         NA      NA       NA    
#---
#Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# 
#Residual standard error: 2.018 on 14 degrees of freedom
#Multiple R-squared:  0.8282,   Adjusted R-squared:  0.8036 
#F-statistic: 33.74 on 2 and 14 DF,  p-value: 4.419e-06

The NA values are there because we have an intercept in the model and the rank of the system is not sufficient to estimate four parameters. You could exclude the intercept using summary(lm(y~x1+x2+x3-1)), but it’s not recommended.

Here is how to do this with a factor variable:

x <- factor(c(1,1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,3))
summary(lm(y~x))

#Call:
#lm(formula = y ~ x)
#
#Residuals:
#   Min     1Q Median     3Q    Max 
#  -3.5   -1.5    0.5    1.5    2.5 
#
#Coefficients:
#            Estimate Std. Error t value Pr(>|t|)    
#(Intercept)  40.5000     0.8238  49.165  < 2e-16 ***
#x2           -1.5000     1.2218  -1.228     0.24    
#x3           -9.0000     1.1650  -7.726 2.05e-06 ***
#---
#Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#
#Residual standard error: 2.018 on 14 degrees of freedom
#Multiple R-squared:  0.8282,   Adjusted R-squared:  0.8036 
#F-statistic: 33.74 on 2 and 14 DF,  p-value: 4.419e-06

Note how the intercept now is the mean for x1 and the other coefficients compare to this mean.