23.20 婴儿的体重随年龄的变化情况

BirthWeight 数据集记录了婴儿的体重随年龄的变化情况,年龄以周为单位计,体重以克为单位计

性别和年龄两个变量,分别是离散型的分类变量和连续型的变量

# 带截距项和不带截距项
summary(l1 <- lm(birthw ~ sex + age), correlation = TRUE)
## 
## Call:
## lm(formula = birthw ~ sex + age)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -257.49 -125.28  -58.44  169.00  303.98 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -1610.28     786.08  -2.049   0.0532 .  
## sexF         -163.04      72.81  -2.239   0.0361 *  
## age           120.89      20.46   5.908 7.28e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 177.1 on 21 degrees of freedom
## Multiple R-squared:   0.64,  Adjusted R-squared:  0.6057 
## F-statistic: 18.67 on 2 and 21 DF,  p-value: 2.194e-05
## 
## Correlation of Coefficients:
##      (Intercept) sexF 
## sexF  0.07            
## age  -1.00       -0.12
anova(l1)
## Analysis of Variance Table
## 
## Response: birthw
##           Df  Sum Sq Mean Sq F value    Pr(>F)    
## sex        1   76163   76163  2.4279    0.1341    
## age        1 1094940 1094940 34.9040 7.284e-06 ***
## Residuals 21  658771   31370                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# 与带交互项的模型比较
summary(li <- lm(birthw ~ sex + sex:age), correlation = TRUE)
## 
## Call:
## lm(formula = birthw ~ sex + sex:age)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -246.69 -138.11  -39.13  176.57  274.28 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -1268.67    1114.64  -1.138 0.268492    
## sexF         -872.99    1611.33  -0.542 0.593952    
## sexM:age      111.98      29.05   3.855 0.000986 ***
## sexF:age      130.40      30.00   4.347 0.000313 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 180.6 on 20 degrees of freedom
## Multiple R-squared:  0.6435, Adjusted R-squared:   0.59 
## F-statistic: 12.03 on 3 and 20 DF,  p-value: 0.000101
## 
## Correlation of Coefficients:
##          (Intercept) sexF  sexM:age
## sexF     -0.69                     
## sexM:age -1.00        0.69         
## sexF:age  0.00       -0.72  0.00
anova(li, l1)
## Analysis of Variance Table
## 
## Model 1: birthw ~ sex + sex:age
## Model 2: birthw ~ sex + age
##   Res.Df    RSS Df Sum of Sq      F Pr(>F)
## 1     20 652425                           
## 2     21 658771 -1   -6346.2 0.1945 0.6639
# 类似,只是使用 glm 命令来拟合而已
summary(zi <- glm(birthw ~ sex + age, family = gaussian()))
## 
## Call:
## glm(formula = birthw ~ sex + age, family = gaussian())
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -257.49  -125.28   -58.44   169.00   303.98  
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -1610.28     786.08  -2.049   0.0532 .  
## sexF         -163.04      72.81  -2.239   0.0361 *  
## age           120.89      20.46   5.908 7.28e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for gaussian family taken to be 31370.04)
## 
##     Null deviance: 1829873  on 23  degrees of freedom
## Residual deviance:  658771  on 21  degrees of freedom
## AIC: 321.39
## 
## Number of Fisher Scoring iterations: 2
anova(zi)
## Analysis of Deviance Table
## 
## Model: gaussian, link: identity
## 
## Response: birthw
## 
## Terms added sequentially (first to last)
## 
## 
##      Df Deviance Resid. Df Resid. Dev
## NULL                    23    1829873
## sex   1    76163        22    1753711
## age   1  1094940        21     658771
# summary(z.o4 <- update(zi, subset = -4))
summary(zz <- update(zi, birthw ~ sex + age + sex:age))
## 
## Call:
## glm(formula = birthw ~ sex + age + sex:age, family = gaussian())
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -246.69  -138.11   -39.13   176.57   274.28  
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -1268.67    1114.64  -1.138 0.268492    
## sexF         -872.99    1611.33  -0.542 0.593952    
## age           111.98      29.05   3.855 0.000986 ***
## sexF:age       18.42      41.76   0.441 0.663893    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for gaussian family taken to be 32621.23)
## 
##     Null deviance: 1829873  on 23  degrees of freedom
## Residual deviance:  652425  on 20  degrees of freedom
## AIC: 323.16
## 
## Number of Fisher Scoring iterations: 2
anova(zi, zz)
## Analysis of Deviance Table
## 
## Model 1: birthw ~ sex + age
## Model 2: birthw ~ sex + age + sex:age
##   Resid. Df Resid. Dev Df Deviance
## 1        21     658771            
## 2        20     652425  1   6346.2