[R-sig-Geo] Help with the Predict function from the Raster package.

Achilleas Psomas axillez at gmail.com
Mon Aug 10 17:00:16 CEST 2009


Dear R-sig Help.

I am trying to use the predict function from the Raster package but I
am running into trouble.
I am attaching the code together with the error message I get at the end.
I apologize that my code is not reproducible at your machines but I
believe that if my error is obvious it will help you to identify it.
Please pay not attention to the model quality or type. I am trying to
keep it as simple as possible to avoid any extra errors.

My R version is 2.8.0 since I found that 2.9.1 has a number of
glitches with installing new packages.
I would greatly appreciate your help:

Achilleas.

#####################################################

#Loading required packages

require(foreign)
Loading required package: foreign
Warning message:
package 'foreign' was built under R version 2.8.1
require(raster)
Loading required package: raster
Loading required package: sp
Loading required package: rgdal
Geospatial Data Abstraction Library extensions to R successfully loaded
Loaded GDAL runtime: GDAL 1.6.1, released 2009/05/11
Path to GDAL shared files: C:/PROGRA~1/R/R-28~1.0/library/rgdal/gdal
Loaded PROJ.4 runtime: Rel. 4.6.1, 21 August 2008
Path to PROJ.4 shared files: C:/PROGRA~1/R/R-28~1.0/library/rgdal/proj

raster version 0.8.9-22 (4-August-2009)
Warning messages:
1: package 'raster' was built under R version 2.9.1
2: package 'sp' was built under R version 2.8.1
3: package 'rgdal' was built under R version 2.9.1

#Importing the data
ndvi_2003177<-raster("ndvi_2003177.txt")
ndvi_2003193<-raster("ndvi_2003193.txt")

NDVI_2003 <-stack(ndvi_2003177,ndvi_2003193)

layerNames(NDVI_2003)
[1] "ndvi_2003177" "ndvi_2003193"

#Importing the DBF with the XY coordinates for sampling
require(foreign)
laque_pt<- read.dbf("laque_points_with_xy.dbf")

laque_xy<-laque_pt[,20:21]

#Sampling values from the raster stack
laque_pt_ndvi <- cbind(ver=laque_pt$PROZ_VERSI,xyValues(NDVI_2003,laque_xy))

names(as.data.frame(laque_pt_ndvi))
[1] "ver"          "ndvi_2003177" "ndvi_2003193"
dim(as.data.frame(laque_pt_ndvi))
[1] 177   3

#Building a simple model
model<-lm(ver~ndvi_2003177+ndvi_2003193,as.data.frame(laque_pt_ndvi))
summary(model)

Call:
lm(formula = ver ~ ndvi_2003177 + ndvi_2003193, data =
as.data.frame(laque_pt_ndvi))

Residuals:
    Min      1Q  Median      3Q     Max
-92.462 -14.689   1.360  13.839  60.360

Coefficients:
             Estimate Std. Error t value Pr(>|t|)
(Intercept)   108.813      6.832  15.928  < 2e-16 ***
ndvi_2003177  -73.003     21.477  -3.399 0.000838 ***
ndvi_2003193  -42.644     20.944  -2.036 0.043261 *
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 23.39 on 174 degrees of freedom
Multiple R-squared: 0.3771,     Adjusted R-squared: 0.3699
F-statistic: 52.67 on 2 and 174 DF,  p-value: < 2.2e-16

#Precicting the model using the raster stack
test.predict<-predict(NDVI_2003,model)
Error in model.frame.default(Terms, newdata, na.action = na.action,
xlev = object$xlevels) :
  object is not a matrix
In addition: Warning message:
'newdata' had 2 rows but variable(s) found have 1 rows



More information about the R-sig-Geo mailing list