How to run the Kennard-Stone algorithm with a multilayer raster?

450 Views Asked by At

I am dealing with a spatial dataset that I need to divide into a training and a validation subset. To be specific, I have a raster with 31 bands; I need to use all of them as parameters for the division of the dataset into the two subsets. I wish to use the Kennard-Stone algorithm for the division, so I have looked into the two existing functions that can be used in R.

The first is the ken.sto in the soil.spec package. The second is the duplex in the cran package. The problem is that both of them require a matrix or a dataframe as the input, while I have a multilayer raster that I can only convert into an array.

Does anyone have any suggestion on how to transform my spatial data, so that it can be used in one of the KS functions?

2

There are 2 best solutions below

1
On BEST ANSWER

If ras is you stack, you can use as.data.frame(ras) to convert your multilayer raster into a data.frame. This will result in a two-dimensional data.frame with n columns (n = raster layers, in your case 31) and m rows (m = number of cells in your raster). Then you should be able to apply the soil.spec function which requires a data.frame as input.

Note: You will however lose the spatial information if you convert your raster to a data.frame. After you have applied your sampling you might want to export the result back as a raster. Here you can use the indices of the data.frame rows to get the values back into the initial raster grid.

1
On

Thank you very much maRtin, you helped me find the right function (and sorry for the late reply). However, I've got another problem now; after converting the rasters into dataframes, I have tried running ken.sto again, and I get another error:

Error in prcomp.default(inp, scale = T) : 
  cannot rescale a constant/zero column to unit variance

Here is part of the summary of the dataframe I have used as input:

 evi_pks_10.1      evi_pks_10.2      evi_pks_10.3      evi_pks_10.4  

    evi_pks_10.5      evi_pks_10.6    
 Min.   :-999.0    Min.   :-999.0    Min.   :-999.0    Min.   :-999      Min.   :-999.0    Min.   :-999     
 1st Qu.:-999.0    1st Qu.:-999.0    1st Qu.:-999.0    1st Qu.:-999      1st Qu.:-999.0    1st Qu.:-999     
 Median :   1.0    Median :  52.0    Median : 116.0    Median :5677      Median : 148.0    Median :2556     
 Mean   :-269.1    Mean   :-189.9    Mean   :-141.7    Mean   :4159      Mean   :-119.6    Mean   :2196     
 3rd Qu.:   1.0    3rd Qu.: 155.0    3rd Qu.: 212.0    3rd Qu.:6744      3rd Qu.: 245.8    3rd Qu.:4073     
 Max.   :   2.0    Max.   : 360.0    Max.   : 360.0    Max.   :9649      Max.   : 299.0    Max.   :7215     
 NA's   :1555628   NA's   :1555628   NA's   :1555628   NA's   :1555628   NA's   :1555628   NA's   :1555628  
  evi_pks_10.7      evi_pks_10.8      evi_pks_10.9     evi_pks_10.10     evi_pks_10.11     evi_pks_10.12 

So, apparently the problem is that I've got NAs?