Tiffany,

your first function (that works) can be adjusted to get r^2 with a function
like this:

funr2 <-function(x) { summary(  lm(x~time) )$r.squared }

You can get the p-value like this:

funp <- function(x) {
           m <- lm(x~time)
           f <- summary(m)$fstatistic
           pf(f[1], f[2], f[3], lower.tail = FALSE)
}

And you can combine the two

funpr2 <- function(x) {
           m <- lm(x~time)
           f <- summary(m)$fstatistic
           p <- pf(f[1], f[2], f[3], lower.tail = FALSE)
           if (isTRUE(p < 0.05)) {
                summary(  lm(x~time) )$r.squared
           }  else { NA }
}

Best, Robert

On Thu, May 3, 2012 at 7:56 AM, tiffanysmith <smithtiffanyt@gmail.com>wrote:

> I've done the following linear regression of my gridded data vs. time:
>
> data<-brick("data.nc")
> time<-seq(1979,2011,1)
> fun.lm<-function(x) {lm(x~time)$coefficients[2]}
> lm.data<-calc(data,fun.lm)
>
> Then, I was trying to get the R^2 values, but keep getting an error:
>
> > fun=function(x){if (in.na
> (x[1])){NA}else{m<-lm(x[1]~x[2])$coefficients[2];
> > summary(m)$r.squared }}
> > r2.data<-calc(data,fun)
> Error in .local(x, fun, ...) : cannot use this function
>
> How do I actually print out the R^2 values? Also, is there a way to mask
> the
> output raster from the linear regression w. a 95%sig level?
>
> --
> View this message in context:
> http://r-sig-geo.2731867.n2.nabble.com/gridded-time-series-analysis-tp5775651p7523889.html
> Sent from the R-sig-geo mailing list archive at Nabble.com.
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo@r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

	[[alternative HTML version deleted]]

