[R-sig-Geo] predict gam model....

Horacio Samaniego horacio.samaniego at gmail.com
Thu Jan 13 20:10:05 CET 2011


Hi,
I'm having some trouble on a very basic operation it seems.... I am
not successful generating GAM predictions on a grid. I suspect an
issue with NA's but are not sure what it is. I get the following
error:

> preds[["gam1"]]=predict(gam4.1,newdata=clima4.1,type="response")*100
Error in `[[<-.data.frame`(`*tmp*`, i, value = c(NA, NA, 0.259444360380994,  :
  replacement has 88486 rows, data has 996705

Any hint on where the discrepancy between rows come from?....
basically,  don't think that I'm fully understanding the error.

Thanks,

Here's a description of the dataset employed.



1st) I adjusted a gam (from mgcv's library)
mod.gam1 <- gam(formula = resp ~ s(tmin_yr_3km) + s(bio4_3km) +
s(bio15_3km) + s(Slope_3km) + s(Roughness_3km) + s(Bare_3km) +
s(Herb_3km) + s(evi_mean) + s(srtm_3km), family = "binomial", data =
data2, method = "GACV.Cp")

2nd) generated grid to hold the prediction (here's the def)
> str(preds)
Formal class 'SpatialGridDataFrame' [package "sp"] with 6 slots
  ..@ data       :'data.frame':	996705 obs. of  0 variables
  ..@ grid       :Formal class 'GridTopology' [package "sp"] with 3 slots
  .. .. ..@ cellcentre.offset: Named num [1:2] -427500 3475500
  .. .. .. ..- attr(*, "names")= chr [1:2] "x" "y"
  .. .. ..@ cellsize         : num [1:2] 3000 3000
  .. .. ..@ cells.dim        : int [1:2] 621 1605
  ..@ grid.index : int(0)
  ..@ coords     : num [1:2, 1:2] -427500 1432500 3475500 8287500
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : NULL
  .. .. ..$ : chr [1:2] "x" "y"
  ..@ bbox       : num [1:2, 1:2] -429000 3474000 1434000 8289000
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:2] "x" "y"
  .. .. ..$ : chr [1:2] "min" "max"
  ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slots
  .. .. ..@ projargs: chr " +proj=utm +south +no_defs +zone=19
+a=6378137 +rf=298.257223563 +towgs84=0.000,0.000,0.000 +to_meter=1"



HERE's are the variables where this grid came from:
> str(clima4.1)
Formal class 'SpatialGridDataFrame' [package "sp"] with 6 slots
  ..@ data       :'data.frame':	996705 obs. of  15 variables:
  .. ..$ tmin_yr_3km  : int [1:996705] NA NA NA NA NA NA NA NA NA NA ...
  .. ..$ tmax_yr_3km  : int [1:996705] NA NA NA NA NA NA NA NA NA NA ...
  .. ..$ bio1_3km     : int [1:996705] NA NA NA NA NA NA NA NA NA NA ...
  .. ..$ bio4_3km     : int [1:996705] NA NA NA NA NA NA NA NA NA NA ...
  .. ..$ bio12_3km    : int [1:996705] NA NA NA NA NA NA NA NA NA NA ...
  .. ..$ bio15_3km    : int [1:996705] NA NA NA NA NA NA NA NA NA NA ...
  .. ..$ Aspect_3km   : num [1:996705] NaN NaN NaN NaN NaN NaN NaN NaN
NaN NaN ...
  .. ..$ Slope_3km    : num [1:996705] NaN NaN NaN NaN NaN NaN NaN NaN
NaN NaN ...
  .. ..$ Roughness_3km: num [1:996705] NaN NaN NaN NaN NaN NaN NaN NaN
NaN NaN ...
  .. ..$ Tree_3km     : int [1:996705] NA NA NA NA NA NA NA NA NA NA ...
  .. ..$ Bare_3km     : int [1:996705] NA NA NA NA NA NA NA NA NA NA ...
  .. ..$ Herb_3km     : int [1:996705] NA NA NA NA NA NA NA NA NA NA ...
  .. ..$ evi_mean     : num [1:996705] NaN NaN NaN NaN NaN NaN NaN NaN
NaN NaN ...
  .. ..$ clpop2008    : num [1:996705] NaN NaN NaN NaN NaN NaN NaN NaN
NaN NaN ...
  .. ..$ srtm_3km     : int [1:996705] NA NA NA NA NA NA NA NA NA NA ...
  ..@ grid       :Formal class 'GridTopology' [package "sp"] with 3 slots
  .. .. ..@ cellcentre.offset: Named num [1:2] -427500 3475500
  .. .. .. ..- attr(*, "names")= chr [1:2] "x" "y"
  .. .. ..@ cellsize         : num [1:2] 3000 3000
  .. .. ..@ cells.dim        : int [1:2] 621 1605
  ..@ grid.index : int(0)
  ..@ coords     : num [1:2, 1:2] -427500 1432500 3475500 8287500
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : NULL
  .. .. ..$ : chr [1:2] "x" "y"
  ..@ bbox       : num [1:2, 1:2] -429000 3474000 1434000 8289000
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:2] "x" "y"
  .. .. ..$ : chr [1:2] "min" "max"
  ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slots
  .. .. ..@ projargs: chr " +proj=utm +south +no_defs +zone=19
+a=6378137 +rf=298.257223563 +towgs84=0.000,0.000,0.000 +to_meter=1"



Data to model:
> str(data2)
'data.frame':	1252 obs. of  18 variables:
 $ tmin_yr_3km  : int  1 1 1 1 1 1 0 0 1 0 ...
 $ tmax_yr_3km  : int  10 11 10 10 9 8 17 18 10 13 ...
 $ bio1_3km     : int  59 65 62 63 56 52 82 94 59 67 ...
 $ bio4_3km     : int  3818 3610 3717 3687 2928 2777 2413 2415 3677 3486 ...
 $ bio12_3km    : int  567 609 509 487 339 567 184 167 568 226 ...
 $ bio15_3km    : int  31 15 17 17 20 16 166 165 16 92 ...
 $ Aspect_3km   : num  136 200 202 112 149 ...
 $ Slope_3km    : num  8.38 2.47 5.39 5.59 1.21 ...
 $ Roughness_3km: num  417.7 141.2 249.4 255.2 70.3 ...
 $ Tree_3km     : int  8 12 7 7 11 34 2 2 14 0 ...
 $ Bare_3km     : int  21 37 18 53 13 37 2 3 17 0 ...
 $ Herb_3km     : int  78 67 83 74 79 65 61 55 79 22 ...
 $ evi_mean     : num  0.191 0.187 0.24 0.214 0.311 ...
 $ clpop2008    : num  0 0.628 1.04 0.339 0 ...
 $ srtm_3km     : int  675 242 389 250 208 185 3492 3288 413 2306 ...
 $ resp         : num  1 1 1 1 1 1 1 1 1 1 ...
 $ X            : num  250553 236521 243073 246358 483071 ...
 $ Y            : num  4781714 4341028 4352441 4337020 4130890 ...

-- 
Instituto de Silvicultura
Universidad Austral de Chile
Isla Teja, Valdivia, Chile

+56 (63) 222480 [of.]
http://www.ecoinformatica.cl



More information about the R-sig-Geo mailing list