How to insert spaces into rasternames? R

Viewed 174

i try to add a space into a layername of a rasterstack

names(predstack)[[1]] <- "MSR670 max"
> names(predstack)[[1]]
[1] "MSR670.max"

I know this is stupid, but i calculated a model which took 7 days for calculation. Unfortunaly one name of the model variables contains a space in the name. Now i want predict on the stack and that is not working, because the following Error appears:

> Prediction2model <- raster::predict(predstack, var2model)
 Error in predict.randomForest(modelFit, newdata) : 
  variables in the training data missing in newdata
> names(predstack)
 [1] "MSR670.max"                                     "GLI201809_means"                               
 [3] "MSR670201809_sd"                                "MVI201805_max"
> var2model$selectedvars
 [1] "MSR670 max"                                     "GLI201809_means"                               
 [3] "MSR670201809_sd"                                "MVI201805_max"

So my Question is: How is it possible to add a space character to a raster layer name? Or is it possible to change the variable name in the random forest model afterwards?

Thank you for any ideas!

2 Answers

The reason why names(predstack)[[1]] <- "MSR670 max" is not working as intended for you is the way the names() function is implemented in the raster package.

Here you can see the method that is applied when calling the names() function on an object of class RasterStack. In line 60 the method calls the validNames() function which is defined here.

validNames() in turn relies on a function called make.names() which basically ensures that there are no white spaces and other unwanted characters within a string (e.g. your raster name).

So looking at the way the names method assigns names to objects of class raster or rasterStack gives a hint on how we might be able to circumvent the issue.

TLDR:

predstack[[1]]@data@names <- "MSR670 max"

should assign the name directly to the raster without using the names() function and the implemented method for it.

Since it's not intended to assign raster names that way there may be problems occurring down the line with this approach, but it might be just enough in your specific situation.

In case someone has the same problem, here is the solution for the problem when there is a space in the model variable names.

It is not possible to change the names in the model. As @Eike showed above, raster layer names can be changed, but they will be changed again by the raster::predict() function the same way.

The only solution is to extract the raster and then predict on the dataframe. Then you can take the values of the dataframe into a raster and have the prediction of the stack as a raster.

Here the solution based on the given example

name <- names(predstack)
varname <- var2model$selectedvars

#initialize progressbar
pb = txtProgressBar(min = 0, max = length(predstack), initial = 0, style = 3)

#extracting rasterdata
for (i in 1:length(predstack)){
  setTxtProgressBar(pb,i)
  #extracting raster values
  print(paste0("Starting extracting raster data: ", Sys.time()))
  df <- as.data.frame(predstack[[i]])
  colnames(df) <-  name[[i]]
  if(i == 1){
    obs <- df
  }
  else{
  print(paste0("Finished extracting raster data: ", Sys.time()))
  obs <- cbind(obs,df)
  }
}

colnames(obs)[[1]] <- varname[1]
pred_stack_df <- predict(var2model, obs)
predraster <- predstack[[1]]
values(predraster) <- pred_stack_df
Related