1

以下のスクリプトは、ピアソン相関を計算するときに同じデータで機能しました。私は最近、共分散行列を作成して pca に入力するように適応させました。フォーラムで、事前に作成された共分散行列を入力するとメモリの問題を回避できる可能性があると読みましたが、私には当てはまりませんでした。共分散行列を実行すると、次のエラーが発生します。

Error: cannot allocate vector of size 1.1 Gb
In addition: Warning messages:
1: In na.omit.default(cbind(x, y)) :
  Reached total allocation of 6141Mb: see help(memory.size)
2: In na.omit.default(cbind(x, y)) :
  Reached total allocation of 6141Mb: see help(memory.size)
3: In na.omit.default(cbind(x, y)) :
  Reached total allocation of 6141Mb: see help(memory.size)
4: In na.omit.default(cbind(x, y)) :
  Reached total allocation of 6141Mb: see help(memory.size)

メモリの問題が発生しないように、これを行うためのより効率的な方法を誰かが提案できますか? 最初に共分散を計算することで、ここで完全にオフベースである場合、それは問題ありません。最終的に必要なのはPCAだけです。私のデータは、arcGIS のラスター形式の 12 個の 1 バンド ラスターであり、それぞれ 581.15 mb と大きいです。どんな助けでも大歓迎です。

library(rgdal)
library(raster)


setwd("K:/Documents/SDSU/Thesis/GIS Data All/GIS Layers/Generated_Layers/GridsForCor")


# List the full path to each raster:
raster_files = c('aspectclp',
                 'lakedistclp',
                 'ocdistclp',
                 'popdenclp',
                 'roaddistclp',
                 'scurveclp',
                 'sdemclp',
                 'solarradclp',
                 'sslopeclp',
                 'vegcatclp',
                 'canopcvrclp',
                 'canophtclp')

cov_matrix <- matrix(NA, length(raster_files), length(raster_files))
for (outer_n in 1:length(raster_files)) {
  outer_raster <- raster(raster_files[outer_n])
  # Start this loop at outer_n rather than 1 so that we don't compute the 
  # same covariance twice. At the end of the loops cov_matrix will be upper 
  # triangular, with the lower triangle all NA, and the diagonal all NA 
  # (since the diagonal would all be 1 anyway).
  for (inner_n in (outer_n):length(raster_files)) {
    # Don't compute correlation of a raster with itself:
    if (inner_n == outer_n) {next}
    inner_raster <- raster(raster_files[inner_n])
    cov_matrix[outer_n, inner_n] <- cov(outer_raster[], inner_raster[], 
                                    use='complete.obs', method = "spearman")
  }
}

pca_matrix <- princomp(raster_files, cor = FALSE, covmat = cov_matrix))

# Writing to a txt file & csv file
write.table(pca_matrix, "PCA.txt", sep="\t", row.names = FALSE)
write.csv(pca_matrix, "PCA.csv") enter code here
4

1 に答える 1

1

ffdf オブジェクトで pca を実行するのと同様の問題がありました。gc()次のように (内部) ループにa を挿入してみてください。

for (inner_n in (outer_n):length(raster_files)) {
  # Don't compute correlation of a raster with itself:
  if (inner_n == outer_n) {next}
  inner_raster <- raster(raster_files[inner_n])
  cov_matrix[outer_n, inner_n] <- cov(outer_raster[], inner_raster[], 
                                use='complete.obs', method = "spearman")
  gc()
}

これにより、ループを続行するのに十分なメモリを解放できる即時のガベージ コレクションが強制さforれます。少なくとも私にとっては、それが機能するのに十分でした。

于 2014-06-25T09:53:49.010 に答える