Reputation: 431
I have 2 rasters and I would like to compute r2 (Rsquared) between them. Not correlation but a value of r2.
>modelledraster
class : RasterLayer
dimensions : 2803, 5303, 14864309 (nrow, ncol, ncell)
resolution : 0.008333333, 0.008333333 (x, y)
extent : 60.85, 105.0417, 15.95833, 39.31667 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
source : memory
names : layer
values : 0, 400 (min, max)
> modelledraster
class : RasterLayer
dimensions : 2803, 5303, 14864309 (nrow, ncol, ncell)
resolution : 0.008333333, 0.008333333 (x, y)
extent : 60.85, 105.0417, 15.95833, 39.31667 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
source : memory
names : VegH
values : 1.874989e-05, 250 (min, max)
What is the simplest way to calculate r2 between these two rasters? Example data:
library(raster)
set.seed(42)
ras1 <- raster(nrow = 10, ncol = 10)
ras2 <- raster(nrow = 10, ncol = 10)
Upvotes: 0
Views: 99
Reputation: 8146
You can use the following code to calculate Rsquare between 2 rasters
#Create 2 rasters
library(raster)
ras1 <- raster(nrow = 10, ncol = 10)
ras2 <- raster(nrow = 10, ncol = 10)
# Assign random cell values
set.seed(42)
values(ras1) <- runif(ncell(ras1))
values(ras2) <- runif(ncell(ras2))
df <- cbind.data.frame(values(ras1), values(ras2))
names(df) <- c("ras1", "ras2")
lm_mod <- lm(ras1~ras2, data = df)
summary(lm_mod)
#> Call:
#> lm(formula = ras1 ~ ras2, data = df)
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -0.54040 -0.25376 0.05857 0.22808 0.44925
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) 0.57525 0.05202 11.058 <2e-16 ***
#> ras2 -0.12545 0.09911 -1.266 0.209
#> ---
#> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#>
#> Residual standard error: 0.2824 on 98 degrees of freedom
#> Multiple R-squared: 0.01609, Adjusted R-squared: 0.006045
#> F-statistic: 1.602 on 1 and 98 DF, p-value: 0.2086
#Or
cor(values(ras1), values(ras2), use="complete.obs", method = 'pearson')^2
#>[1] 0.01608534
You can see that the value Rsquare from lm
and squared cor
function is the same.
Upvotes: 1