[R-sig-ME] Fw: question about Type III sums of squares as computed in "CAR'

Fox, John jfox at mcmaster.ca
Tue Nov 24 18:26:36 CET 2015


Dear Stanley,

I've found it very hard to ascertain from your messages what you've done, which is why I didn't responded earlier. Even this last message didn't come through very clearly, and as far as I can see, you haven't included the data necessary to reproduce what you've done. Maybe I've somehow missed it.

Here, however, is a nonsense example of apparently the same structure as yours using the Mroz data set in the car package:

----------- snip ----------

> library(car)
> Mroz$k <- with(Mroz, factor(k5 != 0))
> options(contrasts=c("contr.sum", "contr.poly"))

> (mod <- lm(log(inc + 1) ~ wc + hc + hc:lfp + wc:k, data=Mroz))

Call:
lm(formula = log(inc + 1) ~ wc + hc + hc:lfp + wc:k, data = Mroz)

Coefficients:
(Intercept)          wc1          hc1    hcno:lfp1   hcyes:lfp1      wcno:k1     wcyes:k1  
   2.976605    -0.087079    -0.184151     0.086135     0.090067     0.076088     0.001294  

> Anova(mod)
Anova Table (Type II tests)

Response: log(inc + 1)
           Sum Sq  Df F value    Pr(>F)    
wc          1.879   1  8.5965  0.003471 ** 
hc         17.084   1 78.1708 < 2.2e-16 ***
hc:lfp      5.297   2 12.1190 6.614e-06 ***
wc:k        1.755   2  4.0146  0.018441 *  
Residuals 163.036 746                      
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

> Anova(mod, type=3)
Anova Table (Type III tests)

Response: log(inc + 1)
            Sum Sq  Df    F value    Pr(>F)    
(Intercept) 3621.1   1 16569.0315 < 2.2e-16 ***
wc             2.5   1    11.4747 0.0007425 ***
hc            16.9   1    77.1361 < 2.2e-16 ***
hc:lfp         5.3   2    12.1190 6.614e-06 ***
wc:k           1.8   2     4.0146 0.0184413 *  
Residuals    163.0 746                         
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
> 

> Mroz2 <- Mroz
> Mroz2[Mroz2$hc=="no" & Mroz2$lfp == "no", "lfp"] <- NA # introduce empty cell

> (mod2 <- lm(log(inc + 1) ~ wc + hc + hc:lfp + wc:k, data=Mroz2))

Call:
lm(formula = log(inc + 1) ~ wc + hc + hc:lfp + wc:k, data = Mroz2)

Coefficients:
(Intercept)          wc1          hc1    hcno:lfp1   hcyes:lfp1      wcno:k1     wcyes:k1  
    2.92816     -0.08765     -0.22868           NA      0.09230      0.08494      0.01049  

> Anova(mod2)
Anova Table (Type II tests)

Response: log(inc + 1)
           Sum Sq  Df F value    Pr(>F)    
wc          1.767   1  8.2230  0.004298 ** 
hc         19.398   1 90.2766 < 2.2e-16 ***
hc:lfp      2.261   1 10.5239  0.001251 ** 
wc:k        1.238   2  2.8799  0.057004 .  
Residuals 116.029 540                      
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

> Anova(mod2, type=3) # fails
Error in Anova.III.lm(mod, error, singular.ok = singular.ok, ...) : 
  there are aliased coefficients in the model

> Anova(mod2, type=3, singular.ok=TRUE)
Anova Table (Type III tests)

Response: log(inc + 1)
             Sum Sq  Df    F value    Pr(>F)    
(Intercept) 2513.20   1 11696.4835 < 2.2e-16 ***
wc             2.19   1    10.1934  0.001492 ** 
hc            20.68   1    96.2614 < 2.2e-16 ***
hc:lfp         2.26   1    10.5239  0.001251 ** 
wc:k           1.24   2     2.8799  0.057004 .  
Residuals    116.03 540                         
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

-------------- snip ------------

Note that you have to use contrasts, such as those generated by contr.sum(), that are orthogonal in the basis of the design matrix for the "type-III" tests computed by Anova() to make sense, and that the computational approach to type-III tests taken by Anova() probably doesn't make sense when there is an empty cell. Note as well that your model isn't a mixed-effects model.

Best,
 John

-----------------------------------------------
John Fox, Professor
McMaster University
Hamilton, Ontario, Canada
http://socserv.socsci.mcmaster.ca/jfox/



> -----Original Message-----
> From: R-sig-mixed-models [mailto:r-sig-mixed-models-bounces at r-
> project.org] On Behalf Of m51988mnew at juno.com
> Sent: Friday, November 20, 2015 12:44 PM
> To: r-sig-mixed-models at r-project.org
> Cc: m51988mnew at juno.com
> Subject: [R-sig-ME] Fw: question about Type III sums of squares as
> computed in "CAR'
> 
> 
> 
> Please note: forwarded message attached
> 
> From: "m51988mnew at juno.com" <m51988mnew at juno.com>
> To: :r-sig-mixed-models at r-project.org
> Cc: m51988mnew at juno.com
> Subject: Fw: question about Type III sums of squares as computed in
> "CAR'
> Date: Fri, 20 Nov 2015 17:41:53 GMT
> 
> SiteDayyoccidbDate      110.004613a01      120.004555a02
> 130.001759a03      210.002755a04      220.00043a05      310.001138a06
> 320.005983a07      330.003027a08      410.001202a09      420.003261a010
> 430.00652a011      110.002368b61      120.001521b62      130.001381b73
> 210.004089b74      220.001139b75      310.000723b76      320.001017b77
> 330.001621b78      410.000562b89      420.002402b810      430.003053b811
> library(car)          exp <-
> read.table(file="example.csv",sep=",",header=TRUE)    exp$Site <-
> as.factor(exp$Site)        exp$Day <- as.factor(exp$Day)        exp$occ
> <- as.factor(exp$occ)        exp$idb  <- as.factor(exp$idb)
> exp$Date <- as.factor(exp$Date)        exp.lm1 <- lm(log(y) ~ occ + Date
> + occ: idb,data=exp)      an_lm13 <- Anova(exp.lm1, type=c("III"))
> an_lm12 <- Anova(exp.lm1, type=c("II"))       exp.lm2 <- lm(log(y) ~ occ
> + Site + Site:Day + occ:idb,data=exp)     an_lm23 <- Anova(exp.lm2, t!
>  ype=c("III"))      an_lm22 <- Anova(exp.lm2, type=c("II"))
> exp.lm3 <- lm(log(y) ~  Site + Site:Day ,data=exp)       an_lm33 <-
> Anova(exp.lm3, type=c("III"))      an_lm32 <- Anova(exp.lm3,
> type=c("II"))                                           r output:
> > library(car)          > exp <-
> read.table(file="example.csv",sep=",",header=TRUE)      > exp$Site <-
> as.factor(exp$Site)        > exp$Day <- as.factor(exp$Day)        >
> exp$occ <- as.factor(exp$occ)        > exp$idb  <- as.factor(exp$idb)
> > exp$Date <- as.factor(exp$Date)        > exp.lm1 <- lm(log(y) ~ occ +
> Date + occ: idb,data=exp)      >  an_lm13 <- Anova(exp.lm1,
> type=c("III"))       Error in Anova.III.lm(mod, error, singular.ok =
> singular.ok, ...) :       there are aliased coefficients in the model
> > an_lm12 <- Anova(exp.lm1, type=c("II"))       > exp.lm2 <- lm(log(y) ~
> occ + Site + Site:Day + occ:idb,data=exp)     >  an_lm23 <-
> Anova(exp.lm2, type=c("III"))       Error in Anova.III.lm(mod!
>  , error, singular.ok = singular.ok, ...) :       there are aliased coe
> fficients in the model       > an_lm22 <- Anova(exp.lm2, type=c("II"))
> > exp.lm3 <- lm(log(y) ~  Site + Site:Day ,data=exp)       >  an_lm33 <-
> Anova(exp.lm3, type=c("III"))       Error in Anova.III.lm(mod, error,
> singular.ok = singular.ok, ...) :       there are aliased coefficients
> in the model       > an_lm32 <- Anova(exp.lm3, type=c("II"))       >
> 
> Please note: forwarded message attached
> 
> From: "m51988mnew at juno.com" <m51988mnew at juno.com>
> To: r-sig-mixed-models at r-project.org
> Subject: Fw: question about Type III sums of squares as computed in
> "CAR'
> Date: Fri, 20 Nov 2015 15:52:48 GMT
> 
> I have attached the example data and the r-code and r messages.The data
> are example measurements of exposures on 2 occupations ( a and b)on 11
> sampling days.  Each occupation  has one measurement per day.  The 11
> sampling days are at 4 sites:  3 days at 3 of the sites and  2 days at 1
> site.  In addition, there were three  workers who did the second
> occupation  , labelled 6,7,8, for the variable idb.  I need the SAS type
> III sums of squares for each of the variables in the model, but you can
> see the error message in the R code: there are aliased coefficients in
> the model In SAS I believe that this problem is dealt with by placing
> constraints on the parameters.I would appreciate any suggestions.  I
> think that maybe I am formulating the model wrong.  Thanks,Stanley
> Shulman
> 
> ---------- Forwarded Message ----------
> From: "m51988mnew at juno.com" <m51988mnew at juno.com>
> To: r-sig-mixed-models at r-project.org
> Subject: question about Type III sums of squares as computed in "CAR'
> Date: Wed, 18 Nov 2015 02:25:03 GMT
> 
> When I use "Anova" from the car library, I cannot get Type III sums of
>  squares for nested data.  Maybe I am using the program wrong. SAS gives
> the
>  Type III sums of squares. Thanks for any thoughts.Stanley Shulman
> 
> ____________________________________________________________
> NetZero now offers 4G mobile broadband. Sign up now.
> http://www.netzero.net/?refcd=NZINTISP0512T4GOUT1
> _______________________________________________
> R-sig-mixed-models at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-mixed-models



More information about the R-sig-mixed-models mailing list