I am trying to calculate a partial correlation between 6 (observed and modelled data) variables in a single NetCDF using ppcor
package in r, but I'm not successful yet. I am still learning loops. This is what I tried so far :
library(ppcor)
mods <- nc_open("Obs_model_1981-2018.nc")
lon <- ncvar_get(mods, varid = "lon")
lat <- ncvar_get(mods, varid = "lat")
nx <- length(lon)
ny <- length(lat)
obss <- ncvar_get(mods, "obs")
mods1 <- ncvar_get(mods, "mod1")
mods2 <- ncvar_get(mods, "mod2")
mods3 <- ncvar_get(mods, "mod3")
mods4 <- ncvar_get(mods, "mod4")
mods5 <- ncvar_get(mods, "mod5")
rl=array(0, c(nx,ny))
err=array(0, c(nx,ny))
for (x in 1:nx) {
print(x)
for (y in 1:ny) {
pp =data.frame(obss,mods1,mods2,mods3,mods4,mods5)
## pcor need a data frame
rl[x,y] = pcor(pp, "pearson")
}
}
question from:https://stackoverflow.com/questions/65943414/partial-correlation-netcdf