[R] Three-way mosaic residual plot with survey data

Achim Zeileis Achim.Zeileis at uibk.ac.at
Tue Dec 30 22:29:52 CET 2014

On Tue, 30 Dec 2014, Mast, Brent D wrote:

> Hi.
> I want to produce a three-way mosaic residual plot with survey data. I'm 
> using the mosaic function from the vcd package for the plot, and the 
> svytable function from the survey package to produce the contingency 
> table. Here is my code for a two-way table (that works) and a three-way 
> table that plots but I think is wrong.
> library(vcat)

vcd...in case anybody else wondered.

> library(survey)
> data(api)
> summary(apiclus1)
> dclus1<-svydesign(id=~dnum, weights=~pw, data=apiclus1, fpc=~fpc)
> summary(dclus1)
> # two-way plot
> st2 <- svytable(~sch.wide+comp.imp,design=dclus1,round=TRUE)
> summary(st2)
> mosaic(st2,main = "Two-Way Mosaic Plot", shade = TRUE, legend = TRUE)

Note that the test statistic and p-value from the summary.svytable 
(calling svychisq) and from mosaic (calling loglin/loglm) are not the 

> # three-way plot
> st3 <- svytable(~sch.wide+comp.imp+stype,design=dclus1,round=TRUE)
> summary(st3)
> windows()
> mosaic(st3,main = "Three-Way Mosaic Plot", shade = TRUE, legend = TRUE)
> When I try to summarize the three-way svytable object "st3", I get the 
> following error:
>> summary(st3)
> Error in svychisq.survey.design(~sch.wide + comp.imp + stype, design = dclus1,  :
>  Only 2-way tables at the moment
> It still produces the three-way mosaic plot. But I don't think it's 
> plotting the residuals correctly because the error message indicates 
> only two-way chi-square tests are allowed.

mosaic() does not know anything about survey tables etc. Hence, it just 
ignores the design associated with the contingency tables st2 and st3 and 
just visualizes them as if they were cross-section data.

> The vcd package will create a mosaic residual plot from a log-linear 
> model estimated by the loglm function from the MASS package. But it 
> won't accept a log-linear model created with the svyloglin function from 
> the survey package. I'm wondering if I could run a log-linear model with 
> svyloglin and transform the output into a table format that the mosaic 
> function will plot.

If you use strucplot() directly (rather than the mosaic() interface) you 
can supply your own 'residuals' and/or 'expected' arguments. These can be 
tables of the same dimension as the table visualized and will be used for 

As I'm not really familiar with these chi-squared tests for survey tables, 
I cannot recommend how to adapt exactly. I guess it would be good to use 
the signed square-root of the contributions to the svy chi-squared 
statistic as "residuals" in the strucplot(). Possibly the default cut-offs 
(+/- 2 and 4) could also be modified.


> Any help would be appreciated.
> Thanks, and happy New Year.
> Brent Mast
> Social Science Analyst
> HUD Office of Policy Development & Research
> Program Monitoring & Research Div.
> 	[[alternative HTML version deleted]]
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list