[R-sig-Geo] Predict GAM model with categorical predictors
Thiago Cesar Lima Silveira
thiagoclsilveira at yahoo.com.br
Mon Nov 10 20:18:46 CET 2014
## Disregard the code sent before, this is the right
Hi,
I'm experiencing some problems to predict a
gam model with factor as predictor to raster stack.
I wrote an example code to illustrate the problem.
This is the error message when I try to predict with a model with factors:
'Error in `[.data.frame`(blockvals, , f[j]) : undefined columns selected
I home someone could help.
Thanks.
Thiago
___________________________________
Thiago Cesar Lima Silveira
Biologist - MSc. Zoology
PhD candidate - Zoology PUCRS
e-mail: thiago.cesar at acad.pucrs.br <mailto:thiago.cesar at acad.pucrs.br>
Skype: thiagocesarls
CV: http://lattes.cnpq.br/5960267776845701 <http://lattes.cnpq.br/5960267776845701>
#####
library(mgcv)
library(raster)
#raster example
v1rst<-raster()
values(v1rst) <- 1:ncell(v1rst)
names(v1rst)<-'v1'
# Example of response variable and predictors
y<-c(1,33,500,700, 334,320, 703,303,3030,3002,200,0,100,100,169)
v1<-c(12,33,544,600, 34,30, 03,3390,3030,302,20,108,170,101,2009)
v2<-c(0,0,0,0,0,0,0,0,1,1,1,1,1,1,1)
v3<-c(12,33,544,600, 34,30, 03,3390,3030,302,20,108,170,101,2009)
df<-data.frame(y, v1, v2)
#GAM model with factor
gam1<-gam(y~s(v1)+factor(v2), data=df)
summary(gam1)
#GAM model without factor
gam2<-gam(y~s(v1), data=df)
summary(gam2)
# data.frame with a constant value
#(of class ’factor’) to pass that on to the predict function.
Method<-factor( '0' ,levels=levels(df$v2)))
add<-data.frame(Method)
#Prediction with factor
p<-predict(v1rst,gam1, type='response', const=add )
#This is the error 'Error in `[.data.frame`(blockvals, , f[j]) : undefined columns selected
#Prediction without factor
p<-predict(v1rst,gam2, type='response')
plot(p)
[[alternative HTML version deleted]]
More information about the R-sig-Geo
mailing list