options(show.signif.stars=FALSE) ### Several models fit to the Formaldehyde data lm1 <- lm(optden ~ 1 + carb, Formaldehyde) ## This formula can also be written optden ~ carb because the ## intercept term is implicit. Some people prefer to include it and ## some don't. lm0 <- lm(optden ~ 0 + carb, Formaldehyde) ## To suppress the intercept term you must include 0 + or - 1, as in ## optden ~ carb - 1, in the formula ## Residual sums of squares for the two models (RSS <- c(lm0 = deviance(lm0), lm1 = deviance(lm1))) ## Comparative anova anova(lm0, lm1) ## Corresponding t-statistic from coefficient table for lm1 coef(summary(lm1)) ## Compare to F statistic in the comparative anova coef(summary(lm1))["(Intercept)", "t value"]^2 ### Testing for non-zero values of coefficients by using an offset ## Suppose we wish to test ## H_0: beta_0 = 0.001 versus H_a: beta_0 != 0.001 ## within the full model lm1 ## We can fit lm2 <- lm(optden ~ 0 + carb + offset(rep(0.001, 6)), Formaldehyde) ## and compare anova(lm2, lm1) ## It may be more obvious to fit the full model as lm3 <- lm(optden ~ 1 + carb + offset(rep(0.001, 6)), Formaldehyde) ## which is equivalent to model lm1 all.equal(fitted(lm1), fitted(lm3)) all.equal(coef(lm1), coef(lm3) + c(0.001, 0)) ## but more obvious about the comparison anova(lm2, lm3) ## and coef(summary(lm3)) coef(summary(lm3))["(Intercept)", "t value"]^2 ## In general, the sequential sums of squares for different terms come ## from the "effects" vector effects(lm1) effects(lm1)[2]^2 # sum of squares due to carb sum(effects(lm1)[-(1:2)]^2) # residual sum of squares anova(lm1) # arranged in an anova table