#Obtaining mean and SD for birthweight and gestation

# Mean and SD for birthweight
mean.bwt.model <- lm(birthweight ~ 1, data = bwt, na.action = na.exclude)
summary(mean.bwt.model)
## 
## Call:
## lm(formula = birthweight ~ 1, data = bwt, na.action = na.exclude)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -3.06429 -0.81429 -0.01429  0.73571  2.73571 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   7.2643     0.2052    35.4   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.33 on 41 degrees of freedom
coef(mean.bwt.model)        
## (Intercept) 
##    7.264286
sd(resid(mean.bwt.model))   
## [1] 1.329739
# Mean and SD for gestation
mean.gestation.model <- lm(gestation ~ 1, data = bwt, na.action = na.exclude)
summary(mean.gestation.model)
## 
## Call:
## lm(formula = gestation ~ 1, data = bwt, na.action = na.exclude)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -6.1905 -1.1905  0.3095  1.8095  5.8095 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  39.1905     0.4079   96.08   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.643 on 41 degrees of freedom
coef(mean.gestation.model)      
## (Intercept) 
##    39.19048
sd(resid(mean.gestation.model)) 
## [1] 2.643336

#regression

reg <- lm(birthweight ~ gestation,
          data = bwt,
          na.action = na.exclude)

summary(reg)
## 
## Call:
## lm(formula = birthweight ~ gestation, data = bwt, na.action = na.exclude)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.69661 -0.78430 -0.02426  0.60604  2.09279 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -6.66019    2.21162  -3.011  0.00449 ** 
## gestation    0.35530    0.05631   6.310 1.73e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.953 on 40 degrees of freedom
## Multiple R-squared:  0.4988, Adjusted R-squared:  0.4863 
## F-statistic: 39.82 on 1 and 40 DF,  p-value: 1.733e-07
bwt$gestation_c <- bwt$gestation - mean(bwt$gestation, na.rm = TRUE)

reg_centered <- lm(birthweight ~ gestation_c, data = bwt, na.action = na.exclude)
summary(reg_centered)
## 
## Call:
## lm(formula = birthweight ~ gestation_c, data = bwt, na.action = na.exclude)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.69661 -0.78430 -0.02426  0.60604  2.09279 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  7.26429    0.14706   49.40  < 2e-16 ***
## gestation_c  0.35530    0.05631    6.31 1.73e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.953 on 40 degrees of freedom
## Multiple R-squared:  0.4988, Adjusted R-squared:  0.4863 
## F-statistic: 39.82 on 1 and 40 DF,  p-value: 1.733e-07
confint(reg_centered)
##                 2.5 %    97.5 %
## (Intercept) 6.9670707 7.5615007
## gestation_c 0.2415002 0.4691049
# show with estimates in one table
cbind(b=coef(reg_centered), confint(reg_centered))
##                     b     2.5 %    97.5 %
## (Intercept) 7.2642857 6.9670707 7.5615007
## gestation_c 0.3553025 0.2415002 0.4691049
# unstandardized
coef(reg_centered)[2]
## gestation_c 
##   0.3553025
# standardized
coef(reg_centered)[2] * (sd(bwt$gestation) / sd(bwt$birthweight))
## gestation_c 
##   0.7062919
# unstandardized
confint(reg_centered)[2,]
##     2.5 %    97.5 % 
## 0.2415002 0.4691049
# standardized
confint(reg_centered)[2,] * (sd(bwt$gestation) / sd(bwt$birthweight))
##     2.5 %    97.5 % 
## 0.4800688 0.9325151
sd.ratio <- sd(bwt$gestation) / sd(bwt$birthweight)

c(beta = coef(reg_centered)[2] * sd.ratio,
  confint(reg_centered)[2,] * sd.ratio)
## beta.gestation_c            2.5 %           97.5 % 
##        0.7062919        0.4800688        0.9325151
# ANOVA table
anova(reg_centered)
## Analysis of Variance Table
## 
## Response: birthweight
##             Df Sum Sq Mean Sq F value    Pr(>F)    
## gestation_c  1 36.165  36.165  39.816 1.733e-07 ***
## Residuals   40 36.332   0.908                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# R-squared (two methods)
summary(reg_centered)$r.squared      
## [1] 0.4988483
cor(predict(reg_centered), bwt$birthweight)^2   
## [1] 0.4988483