Skip to main content
added 36 characters in body
Source Link
Jeffrey Evans
  • 32k
  • 2
  • 48
  • 97

You are overwriting the elements in your list in using SC[] <- .... Look at the result of your list object before and after applying that specific line of code.

Let' recreate a semblance of your model.

library(RStoolbox)
library(raster)

data(rlogo)
train <- readRDS(system.file("external/trainingPoints.rds", 
                 package="RStoolbox"))
SC <- superClass(rlogo, trainData = train, responseCol = "class", 
                 model = "rf", tuneLength = 1, 
                 trainPartition = 0.7)

Now, we simply can extract the prediction to a separate object or you can use a double bracket index SC[["map"]] and not create a new object.

pred <- SC[["map"]]

Is your data in a geographic (lat/long) coordinate projection? The raster::area function is intended to return area in a decimal degree geographic projection, which is honestly not good practice for remote sensing analysis. Besides, the function needs an index to know what cells to look at and not an overall count of cells.

If you are working in a projected coordinate system then simply returning the class counts lets you get to area, based on the known cell resolution. This can be done easily by passing a vector of the predicted raster to table and applying the a cell resolution scaling factor, say 30m^2.

table(pred[])*(30^2) # or
raster::freq(pred)*(30^2)

I am curious as to the point of creating a random sample of 1:3 matching the number of cells in the raster.

You are creating a random sample containing the values of [1,2,3].

rs <- sample(1:3, 100, replace=TRUE)

Now, lets create a vector (p) that we would consider the prediction. And get the counts in relation to the random sample.

p <- sample(1:3, 100, replace=TRUE)
  tapply(p, rs, length)

Now, compare those to the actual counts (note; the second line is a brute force approach to verify the tapply results). The random sample aggregation results do not seem to match the actual counts.

tapply(p, p, length)
  length(p[p == 1])

You are overwriting the elements in your list in using SC[] <- .... Look at the result of your list object before and after applying that specific line of code.

Let' recreate a semblance of your model.

library(RStoolbox)
library(raster)

data(rlogo)
train <- readRDS(system.file("external/trainingPoints.rds", 
                 package="RStoolbox"))
SC <- superClass(rlogo, trainData = train, responseCol = "class", 
                 model = "rf", tuneLength = 1, 
                 trainPartition = 0.7)

Now, we simply can extract the prediction to a separate object or you can use a double bracket index SC[["map"]] and not create a new object.

pred <- SC[["map"]]

Is your data in a geographic (lat/long) coordinate projection? The raster::area function is intended to return area in a decimal degree geographic projection, which is honestly not good practice for remote sensing analysis. Besides, the function needs an index to know what cells to look at and not an overall count of cells.

If you are working in a projected coordinate system then simply returning the class counts lets you get to area, based on the known cell resolution. This can be done easily by passing a vector of the predicted raster to table and applying the a cell resolution scaling factor, say 30m^2.

table(pred[])*(30^2)

I am curious as to the point of creating a random sample of 1:3 matching the number of cells in the raster.

You are creating a random sample containing the values of [1,2,3].

rs <- sample(1:3, 100, replace=TRUE)

Now, lets create a vector (p) that we would consider the prediction. And get the counts in relation to the random sample.

p <- sample(1:3, 100, replace=TRUE)
  tapply(p, rs, length)

Now, compare those to the actual counts (note; the second line is a brute force approach to verify the tapply results). The random sample aggregation results do not seem to match the actual counts.

tapply(p, p, length)
  length(p[p == 1])

You are overwriting the elements in your list in using SC[] <- .... Look at the result of your list object before and after applying that specific line of code.

Let' recreate a semblance of your model.

library(RStoolbox)
library(raster)

data(rlogo)
train <- readRDS(system.file("external/trainingPoints.rds", 
                 package="RStoolbox"))
SC <- superClass(rlogo, trainData = train, responseCol = "class", 
                 model = "rf", tuneLength = 1, 
                 trainPartition = 0.7)

Now, we simply can extract the prediction to a separate object or you can use a double bracket index SC[["map"]] and not create a new object.

pred <- SC[["map"]]

Is your data in a geographic (lat/long) coordinate projection? The raster::area function is intended to return area in a decimal degree geographic projection, which is honestly not good practice for remote sensing analysis. Besides, the function needs an index to know what cells to look at and not an overall count of cells.

If you are working in a projected coordinate system then simply returning the class counts lets you get to area, based on the known cell resolution. This can be done easily by passing a vector of the predicted raster to table and applying the a cell resolution scaling factor, say 30m^2.

table(pred[])*(30^2) # or
raster::freq(pred)*(30^2)

I am curious as to the point of creating a random sample of 1:3 matching the number of cells in the raster.

You are creating a random sample containing the values of [1,2,3].

rs <- sample(1:3, 100, replace=TRUE)

Now, lets create a vector (p) that we would consider the prediction. And get the counts in relation to the random sample.

p <- sample(1:3, 100, replace=TRUE)
  tapply(p, rs, length)

Now, compare those to the actual counts (note; the second line is a brute force approach to verify the tapply results). The random sample aggregation results do not seem to match the actual counts.

tapply(p, p, length)
  length(p[p == 1])
added 790 characters in body
Source Link
Jeffrey Evans
  • 32k
  • 2
  • 48
  • 97

You are overwriting the elements in your list in using SC[] <- .... Look at the result of your list object before and after applying that specific line of code.

Let' recreate a semblance of your model.

library(RStoolbox)
library(raster)

data(rlogo)
train <- readRDS(system.file("external/trainingPoints.rds", 
                 package="RStoolbox"))
SC <- superClass(rlogo, trainData = train, responseCol = "class", 
                 model = "rf", tuneLength = 1, 
                 trainPartition = 0.7)

Now, we simply can extract the prediction to a separate object or you can use a double bracket index SC[["map"]] and not create a new object.

pred <- SC[["map"]]

Is your data in a geographic (lat/long) coordinate projection? The raster::area function is intended to return area in a decimal degree geographic projection, which is honestly not good practice for remote sensing analysis. Besides, the function needs an index to know what cells to look at and not an overall count of cells.

If you are working in a projected coordinate system then simply returning the class counts lets you get to area, based on the known cell resolution. This can be done easily by passing a vector of the predicted raster to table and applying the a cell resolution scaling factor, say 30m^2.

table(pred[])*(30^2)

I am curious as to the point of creating a random sample of 1:3 matching the number of cells in the raster.

You are creating a random sample containing the values of [1,2,3].

rs <- sample(1:3, 100, replace=TRUE)

Now, lets create a vector (p) that we would consider the prediction. And get the counts in relation to the random sample.

p <- sample(1:3, 100, replace=TRUE)
  tapply(p, rs, length)

Now, compare those to the actual counts (note; the second line is a brute force approach to verify the tapply results). The random sample aggregation results do not seem to match the actual counts.

tapply(p, p, length)
  length(p[p == 1])

You are overwriting the elements in your list in using SC[] <- .... Look at the result of your list object before and after applying that specific line of code.

Let' recreate a semblance of your model.

library(RStoolbox)
library(raster)

data(rlogo)
train <- readRDS(system.file("external/trainingPoints.rds", 
                 package="RStoolbox"))
SC <- superClass(rlogo, trainData = train, responseCol = "class", 
                 model = "rf", tuneLength = 1, 
                 trainPartition = 0.7)

Now, we simply can extract the prediction to a separate object or you can use a double bracket index SC[["map"]] and not create a new object.

pred <- SC[["map"]]

Is your data in a geographic (lat/long) coordinate projection? The raster::area function is intended to return area in a decimal degree geographic projection, which is honestly not good practice for remote sensing analysis. If you are working in a projected coordinate system then simply returning the class counts lets you get to area based on the known cell resolution. This can be done easily by passing a vector of the predicted raster to table and applying the a cell resolution scaling factor, say 30m^2.

table(pred[])*(30^2)

You are overwriting the elements in your list in using SC[] <- .... Look at the result of your list object before and after applying that specific line of code.

Let' recreate a semblance of your model.

library(RStoolbox)
library(raster)

data(rlogo)
train <- readRDS(system.file("external/trainingPoints.rds", 
                 package="RStoolbox"))
SC <- superClass(rlogo, trainData = train, responseCol = "class", 
                 model = "rf", tuneLength = 1, 
                 trainPartition = 0.7)

Now, we simply can extract the prediction to a separate object or you can use a double bracket index SC[["map"]] and not create a new object.

pred <- SC[["map"]]

Is your data in a geographic (lat/long) coordinate projection? The raster::area function is intended to return area in a decimal degree geographic projection, which is honestly not good practice for remote sensing analysis. Besides, the function needs an index to know what cells to look at and not an overall count of cells.

If you are working in a projected coordinate system then simply returning the class counts lets you get to area, based on the known cell resolution. This can be done easily by passing a vector of the predicted raster to table and applying the a cell resolution scaling factor, say 30m^2.

table(pred[])*(30^2)

I am curious as to the point of creating a random sample of 1:3 matching the number of cells in the raster.

You are creating a random sample containing the values of [1,2,3].

rs <- sample(1:3, 100, replace=TRUE)

Now, lets create a vector (p) that we would consider the prediction. And get the counts in relation to the random sample.

p <- sample(1:3, 100, replace=TRUE)
  tapply(p, rs, length)

Now, compare those to the actual counts (note; the second line is a brute force approach to verify the tapply results). The random sample aggregation results do not seem to match the actual counts.

tapply(p, p, length)
  length(p[p == 1])
Source Link
Jeffrey Evans
  • 32k
  • 2
  • 48
  • 97

You are overwriting the elements in your list in using SC[] <- .... Look at the result of your list object before and after applying that specific line of code.

Let' recreate a semblance of your model.

library(RStoolbox)
library(raster)

data(rlogo)
train <- readRDS(system.file("external/trainingPoints.rds", 
                 package="RStoolbox"))
SC <- superClass(rlogo, trainData = train, responseCol = "class", 
                 model = "rf", tuneLength = 1, 
                 trainPartition = 0.7)

Now, we simply can extract the prediction to a separate object or you can use a double bracket index SC[["map"]] and not create a new object.

pred <- SC[["map"]]

Is your data in a geographic (lat/long) coordinate projection? The raster::area function is intended to return area in a decimal degree geographic projection, which is honestly not good practice for remote sensing analysis. If you are working in a projected coordinate system then simply returning the class counts lets you get to area based on the known cell resolution. This can be done easily by passing a vector of the predicted raster to table and applying the a cell resolution scaling factor, say 30m^2.

table(pred[])*(30^2)