How to find residual variance of a linear regression model in R?

R ProgrammingServer Side ProgrammingProgramming

The residual variance is the variance of the values that are calculated by finding the distance between regression line and the actual points, this distance is actually called the residual. Suppose we have a linear regression model named as Model then finding the residual variance can be done as (summary(Model)$sigma)**2.

Example

x1<-rnorm(500,5,1)
y1<-rnorm(500,5,2)
Model1<-lm(y1~x1)
summary(Model1)

Call

lm(formula = y1 ~ x1)

Residuals

Min 1Q Median 3Q Max
-5.6621 -1.2257 -0.0272 1.4151 6.6421

Coefficients

Estimate Std. Error t value Pr(>|t|) (Intercept) 5.12511 0.46364 11.054 <2e-16 *** x1 -0.01077 0.09120 -0.118 0.906 --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error − 1.966 on 498 degrees of freedom

Multiple R-squared − 2.798e-05, Adjusted R-squared: -0.00198

F-statistic − 0.01393 on 1 and 498 DF, p-value: 0.9061

Finding the residual variance of the model −

(summary(Model1)$sigma)**2 [1] 3.863416

Example

x2<-rpois(5000,5)
y2<-rpois(5000,2)
Model2<-lm(y2~x2)
summary(Model2)

Call

lm(formula = y2 ~ x2)

Residuals

  Min 1Q Median 3Q Max
-2.0474 -0.9898 0.0030 1.0102 6.0318

Coefficients

Estimate Std. Error t value Pr(>|t|) (Intercept) 1.953861 0.049840 39.203 <2e-16 *** x2 0.007192 0.009125 0.788 0.431 --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error − 1.423 on 4998 degrees of freedom

Multiple R-squared − 0.0001243, Adjusted R-squared: -7.578e-05

F-statistic − 0.6212 on 1 and 4998 DF, p-value: 0.4306

(summary(Model2)$sigma)**2 [1] 2.024254

Example

x3<-runif(5000,2,5)
y3<-runif(5000,2,10)
Model3<-lm(y3~x3)
summary(Model3)

Call

lm(formula = y3 ~ x3)

Residuals

   Min 1Q    Median 3Q   Max
-3.9879 -2.0642 0.0001 2.0438 4.0109

Coefficients

Estimate Std. Error t value Pr(>|t|) (Intercept) 6.004373 0.136367 44.031 <2e-16 *** x3 -0.004376 0.037914 -0.115 0.908 --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error − 2.334 on 4998 degrees of freedom

Multiple R-squared − 2.666e-06, Adjusted R-squared: -0.0001974

F-statistic − 0.01332 on 1 and 4998 DF, p-value: 0.9081

(summary(Model3)$sigma)**2 [1] 5.445925

Example

x4<-rexp(100000,5.5)
y4<-rexp(100000,7.5)
Model4<-lm(y4~x4)
summary(Model4)

Call

lm(formula = y4 ~ x4)

Residuals

  Min 1Q Median 3Q Max
-0.13359 -0.09515 -0.04089 0.05144 1.39856

Coefficients

Estimate Std. Error t value Pr(>|t|) (Intercept) 0.1335960 0.0005972 223.697 <2e-16 *** x4 -0.0010954 0.0023153 -0.473 0.636 --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error − 0.1335 on 99998 degrees of freedom

Multiple R-squared − 2.239e-06, Adjusted R-squared : -7.762e-06

F-statistic − 0.2239 on 1 and 99998 DF, p-value: 0.6361

(summary(Model4)$sigma)**2 [1] 0.01781908

Example

x5<-sample(0:9,25000,replace=TRUE)
y5<-sample(91:99,25000,replace=TRUE) Model5<-lm(y5~x5)
summary(Model5)

Call

lm(formula = y5 ~ x5)

Residuals

  Min 1Q Median 3Q Max
-3.9949 -1.9937 0.0075 2.0093 4.0105

Coefficients

Estimate Std. Error t value Pr(>|t|) (Intercept) 94.989520 0.030168 3148.693 <2e-16 *** x5 0.000595 0.005641 0.105 0.916 --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error − 2.57 on 24998 degrees of freedom

Multiple R-squared − 4.45e-07, Adjusted R-squared : -3.956e-05

F-statistic − 0.01112 on 1 and 24998 DF, p-value − 0.916

(summary(Model5)$sigma)**2 

[1] 6.604745

raja
Published on 14-Oct-2020 12:07:27
Advertisements