R语言回归克里金数组不兼容错误解决及绘图求助
The error you're seeing (non-conformable arrays) happens because your two standard deviation matrices have different dimensions (250×292 vs 1168×1312)—R can't perform element-wise division on matrices that don't match in rows/columns. These matrices are likely underlying raster outputs from your kriging models, so the fix involves aligning their spatial resolution and extent first using raster processing tools.
Here's a step-by-step R solution using the raster package, which is standard for geospatial raster work:
Step 1: Convert your matrices to raster objects
First, you need to wrap your raw matrices into raster objects with proper geospatial metadata (extent, resolution, projection). You'll need to know the geographic bounds, pixel size, and coordinate reference system (CRS) of each kriging output—this info should be available from your original kriging setup.
library(raster) # Create raster for Ordinary Kriging SD # Replace the extent and resolution values with your actual data ext_ok <- extent(-100, -90, 30, 40) # Example: xmin, xmax, ymin, ymax res_ok <- c(0.04, 0.04) # Example: x, y resolution r_ok <- raster(pH.sd, xmn = ext_ok@xmin, xmx = ext_ok@xmax, ymn = ext_ok@ymin, ymx = ext_ok@ymax, res = res_ok) crs(r_ok) <- "+proj=longlat +datum=WGS84" # Replace with your CRS # Create raster for Regression Kriging SD ext_rk <- extent(-100, -90, 30, 40) res_rk <- c(0.01, 0.01) r_rk <- raster(pH.regkrig.sd, xmn = ext_rk@xmin, xmx = ext_rk@xmax, ymn = ext_rk@ymin, ymx = ext_rk@ymax, res = res_rk) crs(r_rk) <- crs(r_ok) # Ensure both rasters share the same CRS
Step 2: Align the rasters (resample and crop if needed)
You need to resample the lower-resolution raster (OK SD) to match the higher-resolution one (Regression Kriging SD), or vice versa. We'll resample the OK raster to match the Regression Kriging raster's resolution and extent:
# Crop both rasters to their overlapping spatial extent (optional but recommended) common_extent <- intersect(ext_ok, ext_rk) r_ok_cropped <- crop(r_ok, common_extent) r_rk_cropped <- crop(r_rk, common_extent) # Resample the OK raster to match the Regression Kriging raster's resolution r_ok_resampled <- resample(r_ok_cropped, r_rk_cropped, method = "bilinear") # Use method="ngb" (nearest neighbor) if you prefer no interpolation for categorical-like data
Step 3: Calculate the ratio and plot
Now that both rasters have matching dimensions, you can compute the ratio and visualize it:
# Calculate the ratio of Regression Kriging SD to Ordinary Kriging SD ratio_raster <- r_rk_cropped / r_ok_resampled # Plot the ratio map plot(ratio_raster, main = "Ratio of Regression Kriging SD to Ordinary Kriging SD", col = viridis::viridis(10)) # Optional: use a nicer color palette
Key Notes:
- Always ensure both rasters use the same coordinate reference system (CRS) before processing—mismatched CRS will cause alignment issues.
- Choose the resampling method based on your data: bilinear interpolation works well for continuous data like SD values, while nearest neighbor is better for discrete data.
内容的提问来源于stack exchange,提问作者Dimitris K

