[r] How to calculate the 95% confidence interval for the slope in a linear regression model in R

Here is an exercise from Introductory Statistics with R:

With the rmr data set, plot metabolic rate versus body weight. Fit a linear regression model to the relation. According to the fitted model, what is the predicted metabolic rate for a body weight of 70 kg? Give a 95% confidence interval for the slope of the line.

rmr data set is in the 'ISwR' package. It looks like this:

> rmr
   body.weight metabolic.rate
1         49.9           1079
2         50.8           1146
3         51.8           1115
4         52.6           1161
5         57.6           1325
6         61.4           1351
7         62.3           1402
8         64.9           1365
9         43.1            870
10        48.1           1372
11        52.2           1132
12        53.5           1172
13        55.0           1034
14        55.0           1155
15        56.0           1392
16        57.8           1090
17        59.0            982
18        59.0           1178
19        59.2           1342
20        59.5           1027
21        60.0           1316
22        62.1           1574
23        64.9           1526
24        66.0           1268
25        66.4           1205
26        72.8           1382
27        74.8           1273
28        77.1           1439
29        82.0           1536
30        82.0           1151
31        83.4           1248
32        86.2           1466
33        88.6           1323
34        89.3           1300
35        91.6           1519
36        99.8           1639
37       103.0           1382
38       104.5           1414
39       107.7           1473
40       110.2           2074
41       122.0           1777
42       123.1           1640
43       125.2           1630
44       143.3           1708

I know how to calculate the predicted y at a given x but how can I calculate the confidence interval for the slope?

This question is related to r statistics linear-regression confidence-interval

The answer is


Let's fit the model:

> library(ISwR)
> fit <- lm(metabolic.rate ~ body.weight, rmr)
> summary(fit)

Call:
lm(formula = metabolic.rate ~ body.weight, data = rmr)

Residuals:
    Min      1Q  Median      3Q     Max 
-245.74 -113.99  -32.05  104.96  484.81 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 811.2267    76.9755  10.539 2.29e-13 ***
body.weight   7.0595     0.9776   7.221 7.03e-09 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

Residual standard error: 157.9 on 42 degrees of freedom
Multiple R-squared: 0.5539, Adjusted R-squared: 0.5433 
F-statistic: 52.15 on 1 and 42 DF,  p-value: 7.025e-09 

The 95% confidence interval for the slope is the estimated coefficient (7.0595) ± two standard errors (0.9776).

This can be computed using confint:

> confint(fit, 'body.weight', level=0.95)
               2.5 % 97.5 %
body.weight 5.086656 9.0324

Examples related to r

How to get AIC from Conway–Maxwell-Poisson regression via COM-poisson package in R? R : how to simply repeat a command? session not created: This version of ChromeDriver only supports Chrome version 74 error with ChromeDriver Chrome using Selenium How to show code but hide output in RMarkdown? remove kernel on jupyter notebook Function to calculate R2 (R-squared) in R Center Plot title in ggplot2 R ggplot2: stat_count() must not be used with a y aesthetic error in Bar graph R multiple conditions in if statement What does "The following object is masked from 'package:xxx'" mean?

Examples related to statistics

Function to calculate R2 (R-squared) in R pandas: find percentile stats of a given column What exactly does numpy.exp() do? Find p-value (significance) in scikit-learn LinearRegression How to plot ROC curve in Python Pandas - Compute z-score for all columns Calculating percentile of dataset column How to normalize an array in NumPy to a unit vector? How to find row number of a value in R code np.mean() vs np.average() in Python NumPy?

Examples related to linear-regression

Accuracy Score ValueError: Can't Handle mix of binary and continuous target TensorFlow: "Attempting to use uninitialized value" in variable initialization gradient descent using python and numpy Adding a regression line on a ggplot How to calculate the 95% confidence interval for the slope in a linear regression model in R What is the difference between linear regression and logistic regression? Multiple linear regression in Python Add regression line equation and R^2 on graph Linear regression with matplotlib / numpy How to force R to use a specified factor level as reference in a regression?

Examples related to confidence-interval

How to calculate the 95% confidence interval for the slope in a linear regression model in R Compute a confidence interval from sample data