我有几个大的栅格,我想在PCA中进行处理(以生成摘要栅格)。我见过几个例子,人们似乎只是简单地调用prcomp或princomp。但是,当我这样做时,我得到以下错误消息:
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,但我真的很想了解为什么上面的方法不起作用。
谢谢!
发布于 2014-01-25 23:01:08
我自己的问题的答案:我最终做了一些略有不同的事情:我没有使用每个栅格像元作为输入(非常大的数据集),而是采样点,运行主成分分析,然后保存输出模型,以便可以对每个格网像元…进行预测也许不是最好的解决方案,但它是有效的:
rasters <- stack(myRasters)
sr <- sampleRandom(rasters, 5000) # sample 5000 random grid cells
# run PCA on random sample with correlation matrix
# retx=FALSE means don't save PCA scores
pca <- prcomp(sr, scale=TRUE, retx=FALSE)
# write PCA model to file
dput(pca, file=paste("./climate/", name, "/", name, "_pca.csv", sep=""))
x <- predict(rasters, pca, index=1:6) # create new rasters based on PCA predictionshttps://stackoverflow.com/questions/19866009
复制相似问题