[R-sig-Geo] Problem with operations on large files ("cannotallocate vector of size xx")

Maurizio Marchi mauriziomarchi85 at gmail.com
Fri Feb 3 12:48:27 CET 2017


Dear Kamil,
I have two suggestions:

1)
to run PCA on a subset of pixels, randomly selected using the sampleRandom
function of the raster package and then calculate the pca components as
rasters. Something like this where "r" is the RasterStack:
Rsampl<-sampleRandom(WCmaps,n)
#here n is the number of points randomly created; try with 5000 but you can
increase and you
#will see that after a specific value the results will not change
significantly so you will stop
pca<-prcomp(Rsampl,scale=T,center=T,retx=T)
#where "scale=T,center=T" is the same of "cor=T" in the princomp function
pca
summary(pca)
rastersPCA<-stack(predict(r,pca,index=1:n,progress='text'))
#here "n" is the number of components you want to create, generally up to
99% of the explained cumulative variance or those who explained at least 1
time the variance

2)
to run PCA on coarser rasters. Again "r" is the original RasterStack
AggregatedRasterStack<-aggregate(r,fact=n,fun=mean)
#n here is the number of pixels to be considered to calculate the mean. For
instance if you have
#a pixel of 1km edge, fact=5 will create a rasterStack with 5 km of spatial
resolution
pca<-prcomp(na.omit(values(AggregatedRasterStack)),scale=T,center=T,retx=T)
#where again "scale=T,center=T" is the same of "cor=T" in the princomp
function
pca
summary(pca)
rastersPCA<-stack(predict(r,pca,index=1:n,progress='text'))
#here "n" is the number of components you want to create, generally up to
99% of the explained cumulative variance or those who explained at least 1
time the variance

Under a statistical point of view, the first is a random sampling while the
second is a sort of regular sampling

Regards, maurizio

--
Maurizio Marchi,
PhD - Forest Science, Ecological Modelling
Council for Agricultural Research and Economics (CREA, Italy)
SkypeID: maurizioxyzhttps://scholar.google.it/citations?user=_2X6fu8AAAAJ&hl=en
#-----#http://ec.europa.eu/environment/life/project/Projects/index.cfm?fuseaction=search.dspPage&n_proj_id=5035www.selpibio.euwww.eufgis.org

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list