【发布时间】:2019-03-18 10:30:36
【问题描述】:
基于此处的示例:
How to perform dimensionality reduction with PCA in R
和
How to reverse PCA and reconstruct original variables from several principal components?
我正在尝试在光栅砖(69 层)上执行 PCA,然后获取领先的 PC,最后仅使用累积比例约为 95% 的 PC 重建原始变量。
library(raster)
library(ncdf4)
ln <- "https://www.dropbox.com/s/d88iuvp9oio14zk/test.nc?dl=1" # ~400 kb size
### DOWNLOAD THE FILE
download.file(ln,
destfile="test.nc",
method="auto")
st <- brick("test.nc")
nlayers(st)
### DO THE PCA
pca <- prcomp(st[])
# to visualize pcs as rasters
x <- predict(st, pca, index=1:4)
spplot(x) # there are the first 4 PCs explaining most of the data.
然后我尝试从前 4 台 PC 中重建原始变量,因为我对这些的空间分布感兴趣:
### PCA DETAILS
summary(pca) # importance of components
plot (pca) # scree plot
loadings(pca) #eigens
mu <- colMeans(as.matrix(st)) # get the column means to use after
#### REDUCTION
nComp <- 4
Xhat <- pca$x[,1:nComp] %*% t(pca$rotation[,1:nComp])
Xhat <- scale(Xhat, center = -mu, scale = FALSE)
在这里,我以为我只会得到前 4 台 PC。但是,我还是像以前一样以 69 结束:
### CHECK THE DIMENSIONS
dim(Xhat)
### THEN CREATE THE RASTER WITH THE PCs
coords <- coordinates(st[[1]]) # get the lon/lat
rst <- cbind(coords, Xhat) # bind the coordinates
rst <- rasterFromXYZ(rst) # create the raster
plot(rst)
我在这里错过了什么?我不是 PCA 方面的专家,但最初的想法是让更少的层能够解释原始数据中的模式。 谢谢!
【问题讨论】: