[R-sig-Geo] problem with predict() in package raster and factor variables

Frede Aakmann Tøgersen frtog at vestas.com
Sun May 1 06:42:51 CEST 2016


Hi Genoveva

You haven't got a response to your question mainly due to a) missing information and b) missing reproducible example.

If you had provided the missing information I guess you would have solved the problem yourself.

I have never used raster::predict() but having a look at man for that function and you error message there is probably some differences between the data used to estimate the random forest model (you call that a subset of the object 'v') and the data in 'subbrick'. You should provide the structure of data used to fit the random forest model and 'subbrick':


> str(v)
> str(subbrick)

Please also show all the relevant R code to obtain what you want in case the error message is not related to difference in the creation of the subset of 'v' and 'subbrick'


Yours sincerely / Med venlig hilsen

Frede Aakmann Tøgersen
Specialist, M.Sc., Ph.D.
Plant Performance & Modeling

Technology & Service Solutions
T +45 9730 5135
M +45 2547 6050
frtog at vestas.com
http://www.vestas.com

Company reg. name: Vestas Wind Systems A/S
This e-mail is subject to our e-mail disclaimer statement.
Please refer to www.vestas.com/legal/notice
If you have received this e-mail in error please contact the sender. 



-----Original Message-----
From: R-sig-Geo [mailto:r-sig-geo-bounces at r-project.org] On Behalf Of Gonzalez-Mirelis Genoveva
Sent: 30. april 2016 12:33
To: r-sig-geo at r-project.org
Subject: [R-sig-Geo] problem with predict() in package raster and factor variables

Dear list,
I am trying to use the function predict() (in package raster), where I supply: the new data as a RasterBrick, the model (as fit in previous steps and using a different dataset), and a few more arguments including the levels of my only one categorical value. Here is the code I'm using:

 r1 <- predict(subbrick,
              CIF.pa,
              type="response", OOB=T, factors=f)

But I keep getting the following error:

Error in checkData(oldData, RET) :

  Classes of new data do not match original data

Here are more details:

> CIF.pa

         Random Forest using Conditional Inference Trees

Number of trees:  1000

Response:  PA
Inputs:  bathy20_1, TerClass, Smax_ann, Smean_ann, Smin_ann, SPDmax_ann, SPDmean_ann, Tmax_ann, Tmean_ann, Tmin_ann
Number of observations:  986

Where 'TerClass' is a categorical variable.

Here is the data used to train CIF.pa:


> str(v)

'data.frame':   1257 obs. of  15 variables:

 $ RefNo      : int  16 16 16 16 17 17 17 17 18 18 ...

 $ PointID    : int  1 2 3 4 5 6 7 8 9 10 ...

 $ Count      : int  0 0 0 0 0 0 0 0 0 0 ...

 $ PA         : int  0 0 0 0 0 0 0 0 0 0 ...

 $ split      : chr  "T" "T" "T" "T" ...

 $ bathy20_1  : num  256 260 252 266 281 ...

 $ TerClass   : num  2 2 1 1 1 2 1 1 3 3 ...

 $ Smax_ann   : num  35.1 35.1 35.1 35.1 35.1 ...

 $ Smean_ann  : num  35.1 35.1 35.1 35.1 35.1 ...

 $ Smin_ann   : num  34.9 34.9 34.9 34.9 35 ...

 $ SPDmax_ann : num  0.379 0.376 0.378 0.372 0.352 ...

 $ SPDmean_ann: num  0.14 0.137 0.14 0.132 0.12 ...

 $ Tmax_ann   : num  6.97 6.92 7.04 6.87 6.68 ...

 $ Tmean_ann  : num  5.76 5.73 5.79 5.71 5.54 ...

 $ Tmin_ann   : num  4.41 4.32 4.52 4.25 4.07 ...



But actually, I used a subset of v to train the model, that where v$split=='T'

Below are the values and class for TerClass for that subset



> unique(v[v$split=='T',7])

[1] 2 1 3 4 6 5

> class(v$TerClass)

[1] "numeric"

And below are the values and class for the corresponding layer of the RasterBrick:

> unique(values(subbrick$TerClass))

[1] 3 1 2 4 5 6

> class(values(subbrick$TerClass))

[1] "numeric"

And finally, here is what f looks like:


> f
$TerClass
[1] 2 1 3 4 6 5

> class(f)
[1] "list"


As far as I can see the classes in OldData and NewData should be the same, but the error persists. Any ideas on what I could be missing?

Unfortunately I am unable to reproduce the problem (I only encounter it when using my data), but any help will be hugely appreciated

Also, I am aware that I asked this question before (Apr 04, 2013; 1:22pm). Unfortunately I haven't gotten very far since then!

Many thanks in advance for any pointers.

Genoveva

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo at r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list