R version 2.11.1 (2010-05-31) Copyright (C) 2010 The R Foundation for Statistical Computing ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > 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))) lm0 lm1 0.0003307264 0.0002992000 > > ## Comparative anova > anova(lm0, lm1) Analysis of Variance Table Model 1: optden ~ 0 + carb Model 2: optden ~ 1 + carb Res.Df RSS Df Sum of Sq F Pr(>F) 1 5 0.00033073 2 4 0.00029920 1 3.1526e-05 0.4215 0.5516 > > ## Corresponding t-statistic from coefficient table for lm1 > coef(summary(lm1)) Estimate Std. Error t value Pr(>|t|) (Intercept) 0.005085714 0.00783368 0.6492115 5.515953e-01 carb 0.876285714 0.01353454 64.7444207 3.409192e-07 > > ## Compare to F statistic in the comparative anova > coef(summary(lm1))["(Intercept)", "t value"]^2 [1] 0.4214755 > > ### 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) Analysis of Variance Table Model 1: optden ~ 0 + carb + offset(rep(0.001, 6)) Model 2: optden ~ 1 + carb Res.Df RSS Df Sum of Sq F Pr(>F) 1 5 0.00031955 2 4 0.00029920 1 2.0347e-05 0.272 0.6295 > > ## 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)) [1] TRUE > all.equal(coef(lm1), coef(lm3) + c(0.001, 0)) [1] TRUE > > ## but more obvious about the comparison > anova(lm2, lm3) Analysis of Variance Table Model 1: optden ~ 0 + carb + offset(rep(0.001, 6)) Model 2: optden ~ 1 + carb + offset(rep(0.001, 6)) Res.Df RSS Df Sum of Sq F Pr(>F) 1 5 0.00031955 2 4 0.00029920 1 2.0347e-05 0.272 0.6295 > ## and > coef(summary(lm3)) Estimate Std. Error t value Pr(>|t|) (Intercept) 0.004085714 0.00783368 0.5215575 6.295245e-01 carb 0.876285714 0.01353454 64.7444207 3.409192e-07 > coef(summary(lm3))["(Intercept)", "t value"]^2 [1] 0.2720222 > > ## In general, the sequential sums of squares for different terms come > ## from the "effects" vector > effects(lm1) (Intercept) carb -1.121458054 0.559955028 0.005139411 0.009915668 0.010691926 -0.007755559 attr(,"assign") [1] 0 1 attr(,"class") [1] "coef" > effects(lm1)[2]^2 # sum of squares due to carb carb 0.3135496 > sum(effects(lm1)[-(1:2)]^2) # residual sum of squares [1] 0.0002992 > anova(lm1) # arranged in an anova table Analysis of Variance Table Response: optden Df Sum Sq Mean Sq F value Pr(>F) carb 1 0.313550 0.313550 4191.8 3.409e-07 Residuals 4 0.000299 0.000075 > > proc.time() user system elapsed 0.540 0.040 0.553