From rdpeng at gmail.com  Tue Jan 23 14:11:56 2007
From: rdpeng at gmail.com (Roger D. Peng)
Date: Tue, 23 Jan 2007 08:11:56 -0500
Subject: [Rd] How to evaluate an lm() object for generating warning
 statement in a function
In-Reply-To: <6.1.0.6.0.20070122233824.01b9c828@mail.utm.utoronto.ca>
References: <6.1.0.6.0.20070122233824.01b9c828@mail.utm.utoronto.ca>
Message-ID: <45B6099C.5090102@gmail.com>

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.

-roger

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.,
> 
> object1<-lm(y~x)
> 
> The function is as follows (some comments included):
> 
> comp.var.estimates<-function(object.lm)
>                  {
>                  anovmod<-anova(object.lm) #get the anova table for the lm
>                  MStreat<-anovmod[1,3]; MSErr<-anovmod[2,3] #extract Mean 
> Squares
>                  dataframe<- as.data.frame(object.lm[13]) #gets the data 
> that went into the lm
>                  ni <- tapply(dataframe[,1], dataframe[,2], length) #number 
> of cases per treatment level
>                  nisq<-ni^2
>                  no<-(1/(length(ni)-1))*(sum(ni)-(sum(nisq)/sum(ni))) 
> #required to calculate variance components
>                  s2a<-((MStreat-MSErr)/no)
>                  stot<-s2a + MSErr
>                  treatvar<-s2a/stot*100  #calculate variance components as 
> a percentage of the total
>                  errorvar<-MSErr/stot*100
>                  list(treat.var.comp=s2a, 
> err.var.comp=MSErr,
>                  p.var.treat=treatvar, p.var.err=errorvar)
>                  }
> comp.var.estimates(object1)
> 
> 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
> warning.
> 
> 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.
> 
> Cheers,
> 
> Mike
> 
> 
> 
> 
> 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
> www.utm.utoronto.ca/~w3rennie 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
> 

-- 
Roger D. Peng  |  http://www.biostat.jhsph.edu/~rpeng/



[Rd] How to evaluate an lm() object for generating warning statement in a function
Roger D. Peng