Skip to content

create rasters using pca components of bioclimatic variables

4 messages · Andre, Robert J. Hijmans

#
Hi,

I performed pca in 4 bioclimatic variables using princomp.

##stack rasters
myExpl <- raster::stack (Bio01,Bio02,Bio03,Bio04)
##read as data.frame
m <- as.data.frame(myExpl)
##pca (here I used formula to activate na.action) 
pca <- princomp(formula = ~., data = m, cor = TRUE, na.action=na.exclude)

Now I would like to create rasters (with the same size as BIO01) for each
component of the PCA to use them as input in biomod2.

What is the best way to do this?

Many thanks,

Andre



--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/create-rasters-using-pca-components-of-bioclimatic-variables-tp7585158.html
Sent from the R-sig-geo mailing list archive at Nabble.com.
#
Andre,

The below is slightly modified from the example in
?raster::predict

library(raster)
logo <- brick(system.file("external/rlogo.grd", package="raster"))
sr <- sampleRegular(logo, min(100000, ncell(logo)))
pca <- prcomp(sr)
x <- predict(logo, pca, index=1:3)
plot(x)


Robert
On Thu, Nov 21, 2013 at 7:25 AM, Andre <pintodasilva.a at gmail.com> wrote:
#
Thanks Robert.

I got this error

x <- predict(Bio01, pca, index=1:3)
Error in predict.princomp(model, blockvals, ...) :
  'newdata' does not have named columns matching one or more of the
original columns

What is missing in the code?

Thanks


On 21 November 2013 18:41, Robert Hijmans [via R-sig-geo] <
ml-node+s2731867n7585159h14 at n2.nabble.com> wrote:

            

  
    
2 days later
#
You are using 'Bio01' where it probably should be 'myExpl'. I would
have thought that this should have been pretty clear from the working
example that I sent.
Robert
On Thu, Nov 21, 2013 at 10:47 AM, Andre <pintodasilva.a at gmail.com> wrote: