I am trying to find the r2 between two rasters. For instance I have two rasters r1 and r2, which code caluclates the r2?
cor(values(r1), values(r2), use="complete.obs", method = 'pearson')
or
stacking the rasters and converting it into dataframe and finding the r2.
stack1 <- stack(r1, r2)
df1 <- data.frame(na.omit(values(stack1)))
head(df1)
valueofdf1 = lm(gc ~ vc, data=df1)
summary(valueofdf1)$r.squared
Both procedures give a different r2. Thats why I am confused. I donot want to find a correlation but i would like to find a value of r2 between two rasters.
First things first, cor
does not return R^2, but R.
As stated in the comments, you are getting the expected output, as R2 = R*R.
As such, both of these methods are interchangeable (for all practical reasons), as long as you do the necessary adjustments.