如何在R中使用netcdf数据进行PCA

时间:2014-10-16 14:52:26

标签: r pca netcdf

我在R中有以下netcdf文件:

"file oceandata.nc has 2 dimensions:"
"lon   Size: 2160"
"lat   Size: 900"
"------------------------"
"file oceandata.nc has 14 variables:"
"float bio1[lon,lat]  Longname:bio1: Annual Mean Temp Missval:1e+30"
"float bio4[lon,lat]  Longname:bio4: Temp Seasonality (standard deviation * 100) Missval:1e+30"
"float bio8[lon,lat]  Longname:bio8: Mean Temp of Wettest Quarter Missval:1e+30"
"float bio9[lon,lat]  Longname:bio9: Mean Temp of Driest Quarter Missval:1e+30"
"float bio10[lon,lat]  Longname:bio10: Mean Temp of Warmest Quarter Missval:1e+30"
"float bio11[lon,lat]  Longname:bio11: Mean Temp of Coldest Quarter Missval:1e+30"
"float bio12[lon,lat]  Longname:bio12: Annual Precipitation Missval:1e+30"
"float bio13[lon,lat]  Longname:bio13: Precipitation of Wettest Month Missval:1e+30"
"float bio14[lon,lat]  Longname:bio14: Precipitation of Driest Month Missval:1e+30"
"float bio15[lon,lat]  Longname:bio15: Precipitation Seasonality (coefficient of variation) Missval:1e+30"
"float bio16[lon,lat]  Longname:bio16: Precipitation of Wettest Quarter Missval:1e+30"
"float bio17[lon,lat]  Longname:bio17: Precipitation of Driest Quarter Missval:1e+30"
"float bio18[lon,lat]  Longname:bio18: Precipitation of Warmest Quarter Missval:1e+30"
"float bio19[lon,lat]  Longname:bio19: Precipitation of Coldest Quarter Missval:1e+30"

我想对文件中的14个变量执行PCA,但我不确定如何解决此问题,或者在我能够执行此操作之前是否需要将数据转换为其他格式。

到目前为止,我已经完成了(下面的错误消息):

ocean <- open.ncdf("oceandata.nc")

bio1 <- get.var.ncdf(nc=ncdf, varid="bio1")

bio4 <- get.var.ncdf(nc=ncdf, varid="bio4")

bio8 <- get.var.ncdf(nc=ncdf, varid="bio8")

bio9 <- get.var.ncdf(nc=ncdf, varid="bio9")

dim(bio1)

[1] 2160 900

class(bio1)

[1]“矩阵”

oceanvars <- cbind(bio1,bio4, bio8, bio9)

colnames(oceanvars) <- c("bio1", "bio4", "bio8", "bio9")

colnames<-中的错误(*tmp*,值= c(“bio1”,“bio4”,“bio8”,“bio9”:'dimnames'的长度[2]不等于数组范围

pairs(oceanvars)

plot.new()出错:数字边距太大

pca1 <- princomp(oceanvars, scores=TRUE, cor=TRUE)

princomp.default中的错误(oceanvars,scores = TRUE,cor = TRUE):'princomp'只能用于比变量更多的单位

任何建议都会非常感谢!

1 个答案:

答案 0 :(得分:1)

为什么你假设cbind 4个矩阵每个900列,导致一个矩阵对象有4列,允许分配c(&#34; bio1&#34;,&#34; bio4&# 34;,&#34; bio8&#34;,&#34; bio9&#34;)作为列名。

据我所知,您有四个变量Annual Mean TempTemp SeasonalityMean Temp of Wettest QuarterMean Temp of Driest Quarter共有1944000个空间对象,您希望通过PCA。

很遗憾,您不提供可重现的示例,而是通过

创建oceanvars
oceanvars <- cbind( c(bio1), c(bio4), c(bio8), c(bio9) )

应该已经做到了。原因是,c()将矩阵融合为一个简单的向量。

更通用,更简洁的程序会将melt您的矩阵与3列data.frames或更大的data.tables进行比较,然后再merge组合lonlat,然后将值列作为矩阵提供给princomp