在R中使用栅格数据集的PCA [英] PCA using raster datasets in R
问题描述
我有几个要在PCA中处理的大型栅格(以生成摘要栅格). 我看过几个例子,人们似乎只是在称prcomp或princomp.但是,当我这样做时,会出现以下错误消息:
I have several large rasters that I want to process in a PCA (to produce summary rasters). I have seen several examples whereby people seem to be simply calling prcomp or princomp. However, when I do this, I get the following error message:
Error in as.vector(data): no method for coercing this S4 class to a vector
示例代码:
files<-list.files() # a set of rasters
layers<-stack(files) # using the raster package
pca<-prcomp(layers)
我尝试使用栅格砖而不是堆栈,但这似乎不是问题.我需要提供什么方法才能将命令转换为矢量格式?我知道有很多方法可以对栅格进行采样并从中运行PCA,但是我真的很想了解为什么上述方法不起作用.
I have tried using a raster brick instead of stack but that doesn't seem to the issue. What method do I need to provide the command so that it can convert the raster data to vector format? I understand that there are ways to sample the raster and run the PCA from that, but I would really like to understand why the above method is not working.
谢谢!
推荐答案
RStoolbox
包例如:
library('raster')
library('RStoolbox')
rasters <- stack(myRasters)
pca1 <- rasterPCA(rasters)
pca2 <- rasterPCA(rasters, nSamples = 5000) # sample 5000 random grid cells
pca3 <- rasterPCA(rasters, norm = FALSE) # without normalization
这篇关于在R中使用栅格数据集的PCA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!