[R-sig-Geo] Predict GAM model with categorical predictors

Dhyey Bhatpuria dhyeybhatpuria at gmail.com
Tue Nov 11 16:57:14 CET 2014


Dear Thiago,
Your prediction raster (here v1rst) should contain three layers/bands with
each named exactly as your predictors (here v1 and v2). Currently it
contains just one raster layer. I would suggest to prepare layers and then
create a raster brick.

Regards,
Dhyey

-- 

Dhyey Bhatpuria,
Research Fellow,
Space Applications Centre, ISRO,
Ahmedabad, India

-----------------------------

Message: 5
Date: Mon, 10 Nov 2014 16:43:46 -0200
From: Thiago Cesar Lima Silveira <thiagoclsilveira at yahoo.com.br
>
To: r-sig-geo at r-project.org
Subject: [R-sig-Geo] Predict GAM model with categorical predictors
Message-ID: <520F8CDB-03A8-49A2-82A2-12139018D430 at yahoo.com.br>
Content-Type: text/plain; charset="UTF-8"

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
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(var.factor<-factor( '0' ,levels=levels(df$v2)))
add<-data.frame(var.factor)

trat<- c(0,1)
trat.list<-list(trat)

#Prediction with factor
   p<-predict(v1rst,gam1, type='response', factors='trat.list' )

#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