# How to find the degrees of freedom of residual from a regression model in R?

To find the degrees of freedom of residual from a regression model, we can use the function df.residual along with the model object.

For example, if we have a regression model stored in an object called Model then the degrees of freedom of residual for the same model can be found by using the command mentioned below −

df.residual(Model)

## Example 1

Following snippet creates a sample data frame −

x1<-rpois(20,2)
y1<-rpois(20,10)
df1<-data.frame(x1,y1)
df1

## Output

The following dataframe is created −

   x1 y1
1  2  5
2  0 14
3  4  9
4  1  9
5  2  5
6  3 10
7  2 10
8  1 13
9  3  6
10 0 13
11 2 10
12 2  6
13 2  6
14 4 11
15 1 10
16 3 12
17 4 10
18 3  7
19 0 14
20 1  7

To create regression model for data in df1, add the following code to the above snippet −

x1<-rpois(20,2)
y1<-rpois(20,10)
df1<-data.frame(x1,y1)
Model1<-lm(y1~x1,data=df1)
summary(Model1)

## Output

If you execute all the above given snippets as a single program, it generates the following output −

Call:
lm(formula = y1 ~ x1, data = df1)

Residuals:
Min   1Q     Median 3Q   Max
-4.350 -2.694 0.650  2.416 3.462

Coefficients:
Estimate  Std.   Error  t value  Pr(>|t|)
(Intercept) 10.9750  1.1809  9.294  2.72e-08  ***
x1         -0.8125   0.4990  -1.628 0.121
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 2.823 on 18 degrees of freedom
Multiple R-squared: 0.1284, Adjusted R-squared: 0.07996
F-statistic: 2.651 on 1 and 18 DF, p-value: 0.1208

To find degrees of freedom for residual in model Model1, add the following code to the above snippet −

df.residual(Model1)


## Output

If you execute all the above given snippets as a single program, it generates the following output −

 18

## Example 2

Following snippet creates a sample data frame −

Response<-rnorm(20)
iv1<-rnorm(20,2,0.24)
iv2<-rnorm(20,10,2.3)
df2<-data.frame(Response,iv1,iv2)
df2

The following dataframe is created −

    Response     iv1         iv2
1  -0.85495077  1.528105   7.705541
2   1.05152113  1.833045   8.553894
3   0.83207179  1.686339  14.282792
4  -0.56286758  1.706313  16.849545
5   2.36207904  2.095680   9.689553
6  -1.83340540  2.576143   9.816262
7   0.60208595  1.940719   8.183135
8   0.85376503  2.132368  11.737135
9  -0.27318666  1.737553  10.880559
10  1.59930686  1.856816  11.051310
11  0.21771933  1.620006  11.755168
12  0.76175494  1.815092  12.406097
13  0.01624025  1.982353  13.062458
14 -0.73307457  2.285373  13.814720
15  0.75039176  1.811981   9.986765
16  0.89032608  1.570384  10.295109
17 -0.60115784  2.179361   9.864406
18  0.49524802  2.114340  11.375692
19  0.19472357  1.249829  12.305074
20 -0.32425762  2.374610  11.766768

To create regression model for data in df2, add the following code to the above snippet −

Response<-rnorm(20)
iv1<-rnorm(20,2,0.24)
iv2<-rnorm(20,10,2.3)
df2<-data.frame(Response,iv1,iv2)
Model2<-lm(Response~iv1+iv2,data=df2)
summary(Model2)

## Output

If you execute all the above given snippets as a single program, it generates the following output −

Call:
lm(formula = Response ~ iv1 + iv2, data = df2)

Residuals:
Min      1Q     Median  3Q      Max
-1.65966 -0.57704 0.04338 0.50260 2.14862

Coefficients:
Estimate Std. Error  t value Pr(>|t|)
(Intercept) 2.54391     1.81171  1.404   0.178
iv1        -0.82378     0.69325 -1.188   0.251
iv2        -0.06234     0.10053 -0.620   0.543

Residual standard error: 0.9618 on 17 degrees of freedom
Multiple R-squared: 0.09101, Adjusted R-squared: -0.01593
F-statistic: 0.851 on 2 and 17 DF, p-value: 0.4444

To find degrees of freedom for residual in model Model2, add the following code to the above snippet −

df.residual(Model2)


## Output

If you execute all the above given snippets as a single program, it generates the following output −

 17

Updated on: 06-Nov-2021

1K+ Views 