rama27
1
Hi,
I have a following problem. I would like to add a variable^2 into OLS model in R. However, in the output the squared variable disappears.
See following simple code:
df <- data.frame(dep = c(20,203,404,502,304,24,25),
name = c("A","A","A","B","B","B","B" ) ,
indep = c(5,7,1,10,6,8,15))
formula = dep ~ indep + indep**2
summary(lm(formula, df))
Call:
lm(formula = formula, data = df)
Residuals:
1 2 3 4 5 6 7
-227.45 -15.02 97.70 328.12 71.27 -179.31 -75.31
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 321.02 163.22 1.967 0.106
indep -14.71 19.31 -0.762 0.481
Residual standard error: 205.9 on 5 degrees of freedom
How is it possible? I want to have estimates of indep, but also indep^2.
Thank you.
You need to use the I()
function to perform arithmetic operations inside a formula.
df <- data.frame(dep = c(20, 203, 404, 502, 304, 24, 25),
name = c("A", "A", "A", "B", "B", "B", "B"),
indep = c(5, 7, 1, 10, 6, 8, 15))
formula <- dep ~ indep + I(indep**2)
summary(lm(formula, df))
#>
#> Call:
#> lm(formula = formula, data = df)
#>
#> Residuals:
#> 1 2 3 4 5 6 7
#> -226.01 -11.75 90.36 330.97 73.83 -175.75 -81.65
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) 331.6090 278.5549 1.190 0.300
#> indep -18.1798 72.1788 -0.252 0.814
#> I(indep^2) 0.2122 4.2160 0.050 0.962
#>
#> Residual standard error: 230.2 on 4 degrees of freedom
#> Multiple R-squared: 0.1046, Adjusted R-squared: -0.3431
#> F-statistic: 0.2336 on 2 and 4 DF, p-value: 0.8018
Created on 2020-06-30 by the reprex package (v0.3.0)
1 Like
system
Closed
3
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.