3
$\begingroup$

I have a random forests model with which I am trying to predict species presence or absence.
This is my code:

#read in dataframe containing observations of species presence/absence & predictor variables
mydata <- read.csv('mydata.csv')

#fit random forests model
fitmodelA <- randomForest(SPECIESA ~ var1 + var2 + var3 + var4 + var5 +var6 + var7 + var8 +
 var9 + var10, data=mydata, mytry=3, ntrees=500, replace=T, importance=T, keep.forest=T)

#predict to new data
predictmodelA <- predict(fitmodelA, newdata, type="prob")

# save as raster image
writeRaster(predictmodelA,"predictSPECIESA.tif")

Apparently I should get back a matrix that has the probability for both classes, i.e., in two columns. Do I understand correctly that it is also possible to add the "index" argument to predict only one class?

With my code the way it is, my output raster produced one layer with probabilities 0 to 1, but no other attributes – what class is this predicting? I am more interested that my map show predictions of presence rather than absence. Probably a simple solution to this...? Thanks!

$\endgroup$
1
$\begingroup$

I don't understand your confusion at all. When you choose type="prob" the output is, naturally, the probability of each class. So you should be getting two columns. You can simply choose either one by doing predictmodelA[,1] or predictmodelA[,2] in your two case example. Since the R vanilla implementation of randomForest needs a factor variable for the case of classification, the order of these probability columns will just follow the order of the factor levels.

By the way I don't think your last bit of code will get you to the raster file you want. You would first need to determine it's coordinates, so you need to add them to your prediction vector, for example:

raster_species <- data.frame(species_prediction=predictmodelA,x=X_coordinate, y=Y_coordinate

then explicitly tell R which variables are the coordinates (I'm guessing you are using the raster package):

coordinates(raster_species)<-~x+y

Indicate that it is gridded

gridded(raster_species)=TRUE

and the simply label it a raster

raster_species <- raster(raster_species)

you can then plot it and save it

plot(raster_species)

writeRaster(raster_species, filename="raster_species.tif", format="GTiff", overwrite=TRUE)
$\endgroup$
  • $\begingroup$ Thank you for your response. The problem I am stuck on is that I don't know which factor is in which index. In other words, what is the order of factors? Is my class 0 index 1, and class 1 = index 2? $\endgroup$ – sth Mar 22 '14 at 19:00
  • $\begingroup$ Note that I am using the predict function from the raster package (i.e., predicting directly from my raster layers) not from a dataframe, because my layers were too big to convert to dataframe. My rasters are floating point data and I think one problem is that there is no attribute table associated with them. $\endgroup$ – sth Mar 22 '14 at 19:30
  • $\begingroup$ if you take your target object and do levels(as.factor(vector)) literally the first level is the first predicted class. $\endgroup$ – JEquihua Mar 23 '14 at 16:43
0
$\begingroup$

This is a partial answer:

What I ended up doing was simply adding the argument

index=1:2

to my predict function.

I haven't figured out how to get a valid dataframe out (because my rasters are floating pt, there is no associated attribute data, i.e., values in the dataframe are all NA when I print it in R) but I can create an output raster just fine.

$\endgroup$
  • $\begingroup$ could you please show this code in its entirety? is it predictmodelA <- predict(fitmodelA, newdata, type="prob", index=1:2) ? $\endgroup$ – Jane Wayne Apr 1 '14 at 12:03
  • $\begingroup$ also, in your example, what is newdata? the manual for this package passes in the training data frame back in. pred <- predict(rfModel, trainData) it's at this point, that i am also confused. pred is a list of probabilities (it seems), but against which field? is it the class field? i would expect something like this. pred <- predict(rfModel, trainData$label) but the package doesn't like that. basically what are those prediction values? which field(s) was the random forest trying to make a prediction on? $\endgroup$ – Jane Wayne Apr 1 '14 at 12:12
  • $\begingroup$ @JaneWayne - yes, the code would be: predictmodelA <- predict(fitmodelA, newdata, type="prob", index=1:2) My NewData was a new database with exactly the same predictors but for an expanded number of rows (i.e., for an expanded area of interest) and no training data was included. $\endgroup$ – sth Apr 9 '14 at 17:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.