PCA using raster datasets in R

后端 未结 6 1685
别那么骄傲
别那么骄傲 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:05

    Here is another approach that expands on the getValues approach proposed by @Daniel. The result is a raster stack. The index (idx) references non-NA positions so that NA values are accounted for.

    library(raster) 
    r <- stack(system.file("external/rlogo.grd", package="raster")) 
    r.val <- getValues(r)
    idx <- which(!is.na(r.val)) 
    pca <- princomp(r.val, cor=T)
    
    ncomp <- 2 # first two principle components
    r.pca <- r[[1:ncomp]]
      for(i in 1:ncomp) { r.pca[[i]][idx] <- pca$scores[,i] } 
    
    plot(r.pca)
    

提交回复
热议问题