Create dataframe from 6 variables in NetCDF

I have a netcdf file with 6 variables and I would like to compute a partial correlation between all variables. I am using the ppcor package and I am having issue to create a dataframe for ppcor. This is how I tried so far but it seems the code is not properly working. can you please help improve the code?

library(ppcor)
library(ncdf4)
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")

pc=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)         
    pc[x,y] = pcor(pp, "pearson")
   }
}

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.