[Rd] How to evaluate an lm() object for generating warning statement in a function
Roger D. Peng
rdpeng at gmail.com
Tue Jan 23 14:11:56 CET 2007
There are two options, you can check to see if 'object.lm' inherits from "lm"
using 'inherits()' or make comp.var.estimates a generic and then make a method
for "lm" objects.
Michael Rennie wrote:
> Hi, there
> I tried this post on R-help but did not generate any replies, so I thought
> I might try the waters here since it's a more programming-based group.
> I have written a function that will allow me to calculate the variance
> components for a model II (random effects) single factor ANOVA (perhaps
> this is
> me re-inventing the wheel, but I handn't yet come across anything in R that
> does this for me, but I'm sure people will let me know from this posting if
> there are). The function takes as it's argument an object which holds the
> results of an lm call. e.g.,
> The function is as follows (some comments included):
> anovmod<-anova(object.lm) #get the anova table for the lm
> MStreat<-anovmod[1,3]; MSErr<-anovmod[2,3] #extract Mean
> dataframe<- as.data.frame(object.lm) #gets the data
> that went into the lm
> ni <- tapply(dataframe[,1], dataframe[,2], length) #number
> of cases per treatment level
> #required to calculate variance components
> stot<-s2a + MSErr
> treatvar<-s2a/stot*100 #calculate variance components as
> a percentage of the total
> p.var.treat=treatvar, p.var.err=errorvar)
> I'd like to include a "warning" statement in the function that
> returns something like 'function requires arguments that are objects of the
> form obj<-lm
> (y~x)', but I need a case to evaluate the object against in order to throw the
> Any advice?
> I feel like my best opportunity is after the first line into the function,
> where I ask for the ANOVA table. Since this is a 2 X 5 table, presumably I
> should be able to evaluate it against the size of that table? Any thoughts on
> how to check that? I welcome any suggestions.
> Michael Rennie
> Ph.D. Candidate, University of Toronto at Mississauga
> 3359 Mississauga Rd. N.
> Mississauga, ON L5L 1C6
> Ph: 905-828-5452 Fax: 905-828-3792
> [[alternative HTML version deleted]]
> R-devel at r-project.org mailing list
Roger D. Peng | http://www.biostat.jhsph.edu/~rpeng/
More information about the R-devel