require("knitr")
## Loading required package: knitr
knitr::opts_chunk$set(
    comment = "#",
    fig.width=8, 
    fig.height=6, 
    cache = TRUE
)

1 Input data

2 Estimating regression coefficients

3 Fitted Values, Residuals and Sum Squares

#    y fitted0 residual0  fitted1  residual1 diff.residual
# 1 64   59.25      4.75 68.92243  -4.922425      9.672425
# 2 87   59.25     27.75 73.56519  13.434811     14.315189
# 3 50   59.25     -9.25 58.08931  -8.089309     -1.160691
# 4 71   59.25     11.75 62.73207   8.267927      3.482073
# 5 44   59.25    -15.25 53.44654  -9.446545     -5.803455
# 6 56   59.25     -3.25 67.37484 -11.374837      8.124837
# 7 42   59.25    -17.25 37.97066   4.029335    -21.279335
# 8 60   59.25      0.75 51.89896   8.101043     -7.351043

Visualize the fitted line, fitted values, and residuals

Definitions of SSR, SSE, SST:

# [1] 1557.5
# [1] 639.0065
# [1] 918.4935

SSR can be computed directly with

# [1] 918.4935

Plot of Residual Sum Squares

Coefficient of Determination: \(R^2\)

# [1] 0.5897229

Mean Sum Squares and F statististic

# [1] 8.624264

p-value to assess whether the relationship exists (statistical significance measure)

# [1] 0.0260588

ANOVA table

#   df       SS      MSS    Fstat   p.value        R2
# 1  1 918.4935 918.4935 8.624264 0.0260588 0.5897229
# 2  6 639.0065 106.5011       NA        NA 0.4102771
# Analysis of Variance Table
# 
# Response: y
#           Df Sum Sq Mean Sq F value  Pr(>F)  
# x          1 918.49  918.49  8.6243 0.02606 *
# Residuals  6 639.01  106.50                  
# ---
# Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

4 Understanding the Sampling Distributions of SS and F

When the slope is 0

# (Intercept)           x 
#   76.660365   -1.547588
# 
# Call:
# lm(formula = y ~ x)
# 
# Coefficients:
# (Intercept)            x  
#       76.66         0.00
# Analysis of Variance Table
# 
# Response: sim.y
#           Df Sum Sq Mean Sq F value Pr(>F)
# x          1   0.54   0.542  0.0073 0.9345
# Residuals  6 443.18  73.864

When the slope is non-zero

# (Intercept)           x 
#   76.660365   -1.547588
# 
# Call:
# lm(formula = y ~ x)
# 
# Coefficients:
# (Intercept)            x  
#       76.66        -2.00
# Analysis of Variance Table
# 
# Response: sim.y
#           Df Sum Sq Mean Sq F value  Pr(>F)  
# x          1 874.22  874.22  7.4935 0.03385 *
# Residuals  6 699.98  116.66                  
# ---
# Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1