Re: [R-sig-Geo] regression-kriging and co-kriging (Edzer Pebesma)

2019-08-15 Thread Edzer Pebesma


On 8/15/19 12:31 PM, Emanuele Barca wrote:
> Dear Edzer,
> 
> sorry for bothering you once more but I need to be sure about my R script.
> 
> In summary, i'm comparing the performance of regression-kriging and
> collocated co-kriging.
> 
> Regression-kriging was based on an unique covariate, the elevation Z.
> 
> I use Z as unique ancillary variable in the Co-kriging.
> 
> As first attempt, the final raster maps were completely different. It
> appeared that it was due
> 
> to the fact that the dataset was non-stationary and only
> regression-kriging overcomes this issue, while co-kriging not.
> 
> But if I pass to universal co-kriging introducing Z as covariate, it
> bacomes useless as ancillary variable!
> 
> What is my mistake?

You assume that someone else can be sure about your analysis by looking
at your R script without having access to your data.

And you are starting a personal dialogue on a list, essentially
uninviting everyone else to get involved.

> 
> emanuele
> 
> 
> 
> 
> Il 2019-08-15 12:00 r-sig-geo-requ...@r-project.org ha scritto:
>> Send R-sig-Geo mailing list submissions to
>> r-sig-geo@r-project.org
>>
>> To subscribe or unsubscribe via the World Wide Web, visit
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>> or, via email, send a message with subject or body 'help' to
>> r-sig-geo-requ...@r-project.org
>>
>> You can reach the person managing the list at
>> r-sig-geo-ow...@r-project.org
>>
>> When replying, please edit your Subject line so it is more specific
>> than "Re: Contents of R-sig-Geo digest..."
>>
>>
>> Today's Topics:
>>
>>    1. Error running codes (Enoch Gyamfi Ampadu)
>>    2. Re: regression-kriging and co-kriging (Edzer Pebesma)
>>
>> --
>>
>> Message: 1
>> Date: Thu, 15 Aug 2019 06:51:49 +0200
>> From: Enoch Gyamfi Ampadu 
>> To: r-sig-geo@r-project.org
>> Subject: [R-sig-Geo] Error running codes
>> Message-ID:
>> 
>> Content-Type: text/plain; charset="utf-8"
>>
>> Dear List,
>>
>> Please I have been running a certain the codes below to read my image,
>> shapeffile to enable me classify for cover with Random forest. I have
>> gotten to the point of extracting the raster values and the raster
>> information. However I keep getting errors. though I have tried different
>> combination and other codes like readOGR for importing the training
>> polygon. I will be glad if you could be of help as I am new to using R.
>>
>> #import clip image of study area
>>
>> TestImg3 <- brick("C:\\Users\\hp\\Desktop\\Data collection\\Nkandla
>> Images\\Landsat\\2019\\08052019\\Corrected\\New
>> Folder\\Image08052019_Clip.tif")
>>
>> #Assign name of bands
>> names(TestImg3) <- c(paste0("B", 1:7, coll=""), "B9")
>> plotRGB(TestImg3, r=4, g=3, b=2, stretch ="lin")
>>
>> #Import training shapefile
>> sample <- shapefile("C:/Users/hp/Desktop/Data collection/Nkandla
>> Images/Landsat/2019/08052019/Corrected/Train08052019_2.shp")
>>
>> responseCol <- "class"
>>
>> #(I have tried options of changing "class" to "classname" as reflect
>> actaul
>> name assigned in ArcMap)
>>
>> # Overlap the sample polygons on the image (combine the class information
>> with extracted values).
>>
>> pixels = data.frame(matrix(vector(), nrow = 0, ncol =
>> length(names(img)) +
>> 1))
>> for (i in 1:length(unique(sample[[responseCol]]))){
>>   category <- unique(sample[[responseCol]])[i]
>>   categorymap <- sample[sample[[responseCol]] == category,]
>>   dataSet <- extract(img, categorymap)
>>   dataSet <- dataSet[!unlist(lapply(dataSet, is.null))]
>>   if(is(sample, "SpatialPointsDataFrame")){
>>     dataSet <- cbind(dataSet, class = as.numeric(category))
>>     pixeles <- rbind(pixeles, dataSet)
>>   }
>>   if(is(sample, "SpatialPolygonsDataFrame")){
>>     dataSet <- lapply(dataSet, function(x){cbind(x, class =
>> as.numeric(rep(category,
>>
>>  nrow(x})
>>     df <- do.call("rbind", dataSet)
>>     pixels <- rbind(pixeles, df)
>>   }
>>
>> }
>>
>> THIS IS THE ERROR I GET FROM RUNNING THE ABOVE CODES
>>
>>> for (i in 1:length(unique(samples[[responseCol]]))){
>> +   category <- unique(samples[[responseCol]])[i]
>> +   categorymap <- samples[samples[[responseCol]] == category,]
>> +   dataSet <- extract(img, categorymap)
>> +
>> +   if(is(sample, "SpatialPointsDataFrame")){
>> + dataSet <- cbind(dataSet, class = as.numeric(category))
>> + pixeles <- rbind(pixeles, dataSet)
>> +   }
>> +   if(is(sample, "SpatialPolygonsDataFrame")){
>> + dataSet <- lapply(dataSet, function(x){cbind(x, class =
>> as.numeric(rep(category,
>> +
>>   nrow(x})
>> + df <- do.call("rbind", dataSet)
>> + pixels <- rbind(pixeles, df)
>> +   }
>> +
>> + }
>> Error in y[i, ] :
>>   cannot get a slot ("Polygons") from an object of type "NULL"
>>
>>
>> Please help me out.
>> Thank you.
>>
>> Best regards,
>>
>> Enoch
>>
>> -- 
>> *Enoch Gyamfi - Ampadu*
>>
>> *Geography & Environmental Sciences*
>>
>> *College of Agriculture

Re: [R-sig-Geo] regression-kriging and co-kriging (Edzer Pebesma)

2019-08-15 Thread Emanuele Barca

Dear Edzer,

sorry for bothering you once more but I need to be sure about my R 
script.


In summary, i'm comparing the performance of regression-kriging and 
collocated co-kriging.


Regression-kriging was based on an unique covariate, the elevation Z.

I use Z as unique ancillary variable in the Co-kriging.

As first attempt, the final raster maps were completely different. It 
appeared that it was due


to the fact that the dataset was non-stationary and only 
regression-kriging overcomes this issue, while co-kriging not.


But if I pass to universal co-kriging introducing Z as covariate, it 
bacomes useless as ancillary variable!


What is my mistake?

emanuele




Il 2019-08-15 12:00 r-sig-geo-requ...@r-project.org ha scritto:

Send R-sig-Geo mailing list submissions to
r-sig-geo@r-project.org

To subscribe or unsubscribe via the World Wide Web, visit
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
or, via email, send a message with subject or body 'help' to
r-sig-geo-requ...@r-project.org

You can reach the person managing the list at
r-sig-geo-ow...@r-project.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of R-sig-Geo digest..."


Today's Topics:

   1. Error running codes (Enoch Gyamfi Ampadu)
   2. Re: regression-kriging and co-kriging (Edzer Pebesma)

--

Message: 1
Date: Thu, 15 Aug 2019 06:51:49 +0200
From: Enoch Gyamfi Ampadu 
To: r-sig-geo@r-project.org
Subject: [R-sig-Geo] Error running codes
Message-ID:

Content-Type: text/plain; charset="utf-8"

Dear List,

Please I have been running a certain the codes below to read my image,
shapeffile to enable me classify for cover with Random forest. I have
gotten to the point of extracting the raster values and the raster
information. However I keep getting errors. though I have tried 
different

combination and other codes like readOGR for importing the training
polygon. I will be glad if you could be of help as I am new to using R.

#import clip image of study area

TestImg3 <- brick("C:\\Users\\hp\\Desktop\\Data collection\\Nkandla
Images\\Landsat\\2019\\08052019\\Corrected\\New
Folder\\Image08052019_Clip.tif")

#Assign name of bands
names(TestImg3) <- c(paste0("B", 1:7, coll=""), "B9")
plotRGB(TestImg3, r=4, g=3, b=2, stretch ="lin")

#Import training shapefile
sample <- shapefile("C:/Users/hp/Desktop/Data collection/Nkandla
Images/Landsat/2019/08052019/Corrected/Train08052019_2.shp")

responseCol <- "class"

#(I have tried options of changing "class" to "classname" as reflect 
actaul

name assigned in ArcMap)

# Overlap the sample polygons on the image (combine the class 
information

with extracted values).

pixels = data.frame(matrix(vector(), nrow = 0, ncol = 
length(names(img)) +

1))
for (i in 1:length(unique(sample[[responseCol]]))){
  category <- unique(sample[[responseCol]])[i]
  categorymap <- sample[sample[[responseCol]] == category,]
  dataSet <- extract(img, categorymap)
  dataSet <- dataSet[!unlist(lapply(dataSet, is.null))]
  if(is(sample, "SpatialPointsDataFrame")){
dataSet <- cbind(dataSet, class = as.numeric(category))
pixeles <- rbind(pixeles, dataSet)
  }
  if(is(sample, "SpatialPolygonsDataFrame")){
dataSet <- lapply(dataSet, function(x){cbind(x, class =
as.numeric(rep(category,

 nrow(x})
df <- do.call("rbind", dataSet)
pixels <- rbind(pixeles, df)
  }

}

THIS IS THE ERROR I GET FROM RUNNING THE ABOVE CODES


for (i in 1:length(unique(samples[[responseCol]]))){

+   category <- unique(samples[[responseCol]])[i]
+   categorymap <- samples[samples[[responseCol]] == category,]
+   dataSet <- extract(img, categorymap)
+
+   if(is(sample, "SpatialPointsDataFrame")){
+ dataSet <- cbind(dataSet, class = as.numeric(category))
+ pixeles <- rbind(pixeles, dataSet)
+   }
+   if(is(sample, "SpatialPolygonsDataFrame")){
+ dataSet <- lapply(dataSet, function(x){cbind(x, class =
as.numeric(rep(category,
+
  nrow(x})
+ df <- do.call("rbind", dataSet)
+ pixels <- rbind(pixeles, df)
+   }
+
+ }
Error in y[i, ] :
  cannot get a slot ("Polygons") from an object of type "NULL"


Please help me out.
Thank you.

Best regards,

Enoch

--
*Enoch Gyamfi - Ampadu*

*Geography & Environmental Sciences*

*College of Agriculture, Engineering & Science*

*University of KwaZulu-Natal, Westville Campus*

*Private Bag X54001*
*Durban, South Africa **– 4000**.*
*Phone: +27 835 828255*

*email: egamp...@gmail.com *


*skype: enoch.ampadu*
*The highest evidence of nobility is self-control*.

*A simple act of kindness creates an endless ripple*.

[[alternative HTML version deleted]]




--

Message: 2
Date: Thu, 15 Aug 2019 08:33:59 +0200
From: Edzer Pebesma 
To: r-sig-geo@r-project.org
Subject: Re: [R-sig-Geo] regression-kriging and co-kriging
Message-ID: 
Content-Type: text/plain; charset="utf-8"



On 8/12/19 8:21 PM, Emanu