[R] r2 question

Steve Lianoglou mailinglist.honeypot at gmail.com
Sat Jul 25 18:55:56 CEST 2009


Hi Sarah,

On Jul 25, 2009, at 8:25 AM, Buckmaster, Sarah wrote:

> Hi everyone,
>
> I have a question about calculating r-squared in R. I have tried  
> searching the archives and couldn't find what I was looking for -  
> but apologies if there is somewhere I can find this...
>
> I carried out a droughting experiment to test plant competition  
> under limited water. I had:
> - 7 different levels of watering treatment (1 -7 - from most watered  
> to least watered/)
> - 15 replicates at each level.
>
> Soil moisture readings were taken 4 times throughout the experiment  
> (so I have 105 readings for each of the 4 times) and I now want to  
> check that there was a significant decrease in soil moisture as I  
> decreased the watering frequency, i.e. watering level 7 showed lower  
> soil moisture units than level 1.
>
> I have carried out a repeated measures anova as follows (where block  
> is which time the reading was taken: 1,2,3 or 4):
> model1<-aov(soilmoisture~wateringlevel+Error(block/wateringlevel))
>
> I then plotted (soilmoisture~wateringlevel) and fitted a regression  
> line:
> lm1<-lm(soilmoisture~wateringlevel)
> abline(lm1,lty=1)
>
>
> Here are my questions:
>
> 1) Is the repeated measures anova I have entered correct to tell me if
> there is a significant difference in my watering levels?

It seems like using anova here is reasonable -- I haven't used it in  
R, though, so I can't comment on your *actual* use of it (looks right  
-- can't u also just call aov on your lm1 object, too?)

> 2) How do I calculate r2 value to show much variation my watering
> level explains? - and then put this figure on my plot?

I think this should be pretty straight forward -- you can find the  
formula here:

http://en.wikipedia.org/wiki/Fraction_of_variance_unexplained

and R^2 = SSR / SST (on that page)

Using the formulas on that page, translating to R is pretty straight  
forward. Let's take the example code at the bottom of ?lm help page  
for reference and calculate the R^2:

ssr <-  sum((predict(lm.D9, group) - weight)^2)
sst <- sum((weight - mean(weight))^2)
r2 <- ssr / sst

HTH,
-steve

--
Steve Lianoglou
Graduate Student: Computational Systems Biology
   |  Memorial Sloan-Kettering Cancer Center
   |  Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact




More information about the R-help mailing list