[R] models for square tables

John Hendrickx john_hendrickx at yahoo.com
Thu Feb 13 15:21:03 CET 2003


Labels can be helpful but can also cause messy output. In some of the
other functions, I added an option to suppress printing labels,
especially for symmetrical interactions.

A problem (in my view) with your approach is that it sorts the
categories according to the level names. I'd prefer the following,
which would make labels optional but otherwise maintain the original
order.

mob.qi <- function
(rowvar,colvar,constrained=FALSE,print.labels=FALSE) {
	check.square(rowvar,colvar)
	if (constrained) {
		qi <- ifelse(rowvar==colvar, 1, 0)
		nms<-c("diagonal")
	}
	else {
		qi <- ifelse(rowvar==colvar, rowvar, 0)
		nms<-levels(rowvar)
	}

	qi<-factor(qi)
	qi<-C(qi,contr.treatment,base=1)
	if (print.labels) {
		levels(qi)<-c("offdiag",nms)
	}
	qi
}

Thanks for your suggestions and for pointing out "relevel" to me!

John Hendrickx

--- David Firth <david.firth at nuffield.oxford.ac.uk> wrote:
> Nice.
> 
> I guess I normally do things a little bit differently, to get 
> coefficient names that look a bit more meaningful (eg avoiding
> numeric 
> codes for factor levels).
> 
> For example one possible adjustment to your code for the qi models 
> would be
> 
> mob.qi <- function(rowvar, colvar, constrained=FALSE) {
> 	check.square(rowvar, colvar)
> 	if (!constrained) {
> 		qi <- ifelse(rowvar==colvar, as.character(rowvar), "offdiag")
> 		relevel(factor(qi), "offdiag")
> 	} else qi <- ifelse(rowvar==colvar, 1, 0)
> }
> 
> immobile <- mob.qi(OccFather, OccSon)
> glm.qi2<-glm(Freq ~ OccFather + OccSon + immobile,
> family=poisson())
> 
> immobile <- mob.qi(OccFather, OccSon, constrained=T)
> glm.q02 <-glm(Freq ~ OccFather + OccSon + immobile,
> family=poisson())
> 
> Cheers,
> David
> 
> On Wednesday, Feb 12, 2003, at 13:01 Europe/London, John Hendrickx 
> wrote:
> 
> > I've posted a sample file for estimating loglinear models for
> square
> > tables (mobility models) at http://www.xs4all.nl/~jhckx/mcl/R/
> > Comments and suggestions are welcome.
> >
> > John Hendrickx
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > http://www.stat.math.ethz.ch/mailman/listinfo/r-help
> >
>




More information about the R-help mailing list