PCA using raster datasets in R

后端 未结 6 1642
别那么骄傲
别那么骄傲 2021-01-18 10:48

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 pr

6条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-18 11:12

    Answer to my own question: I ended up doing something slightly different: rather than using every raster cell as input (very large dataset), I took a sample of points, ran the PCA and then saved the output model so that I could make predictions for each grid cell…maybe not the best solution but it works:

    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 predictions
    

提交回复
热议问题