[R] Advice on an appropriate condition for warning in a function

Mike Rennie mrennie at utm.utoronto.ca
Mon Jan 22 06:05:18 CET 2007



Hi, there

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]) 		
		ni <- tapply(dataframe[,1], dataframe[,2], length) 		
		nisq<-ni^2
		no<-(1/(length(ni)-1))*(sum(ni)-(sum(nisq)/sum(ni)))
		s2a<-((MSTreat-MSErr)/no)
		stot<-s2a + MSErr
		treatvar<-s2a/stot*100 
		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 '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



More information about the R-help mailing list