Thank you Robert.

I had this before but was just not sure if it shows me exactly the classes I wanted.
Many thanks to you!!!


Von: Robert J. Hijmans [mailto:r.hijmans@gmail.com]
Gesendet: Donnerstag, 31. Januar 2013 19:20
An: Reichmuth, Anne
Cc: r-sig-geo@r-project.org
Betreff: Re: [R-sig-Geo] probability predict by class

Anne, please read carefully the help (and examples) in ?raster::predict. I think what you are after is the argument "index=1:3". Robert
On Thu, Jan 31, 2013 at 9:14 AM, <Anne.Reichmuth@dlr.de<mailto:Anne.Reichmuth@dlr.de>> wrote:
Hello Robert,
thank you, this now works. But it just calculates the probability for the first class, at least it seems like it.
How can I calculate respectively plot it for all three classes separately?

Here is the structure of Rprob:

Formal class 'RasterLayer' [package "raster"] with 12 slots
  ..@ file    :Formal class '.RasterFile' [package "raster"] with 12 slots
  .. .. ..@ name        : chr ""
  .. .. ..@ datanotation: chr "FLT4S"
  .. .. ..@ byteorder   : chr "little"
  .. .. ..@ nodatavalue : num -Inf
  .. .. ..@ NAchanged   : logi FALSE
  .. .. ..@ nbands      : int 1
  .. .. ..@ bandorder   : chr "BIL"
  .. .. ..@ offset      : int 0
  .. .. ..@ toptobottom : logi TRUE
  .. .. ..@ blockrows   : int 0
  .. .. ..@ blockcols   : int 0
  .. .. ..@ driver      : chr ""
  ..@ data    :Formal class '.SingleLayerData' [package "raster"] with 13 slots
  .. .. ..@ values    : num [1:3766480] NA NA NA NA NA NA NA NA NA NA ...
  .. .. ..@ offset    : num 0
  .. .. ..@ gain      : num 1
  .. .. ..@ inmemory  : logi TRUE
  .. .. ..@ fromdisk  : logi FALSE
  .. .. ..@ isfactor  : logi FALSE
  .. .. ..@ attributes: list()
  .. .. ..@ haveminmax: logi TRUE
  .. .. ..@ min       : num 0.000495
  .. .. ..@ max       : num 0.996
  .. .. ..@ band      : int 1
  .. .. ..@ unit      : chr ""
  .. .. ..@ names     : chr ""
  ..@ legend  :Formal class '.RasterLegend' [package "raster"] with 5 slots
  .. .. ..@ type      : chr(0)
  .. .. ..@ values    : logi(0)
  .. .. ..@ color     : logi(0)
  .. .. ..@ names     : logi(0)
  .. .. ..@ colortable: logi(0)
  ..@ history : list()
  ..@ title   : chr(0)
  ..@ extent  :Formal class 'Extent' [package "raster"] with 4 slots
  .. .. ..@ xmin: num 771779
  .. .. ..@ xmax: num 776011
  .. .. ..@ ymin: num 5305525
  .. .. ..@ ymax: num 5309085
  ..@ rotated : logi FALSE
  ..@ rotation:Formal class '.Rotation' [package "raster"] with 2 slots
  .. .. ..@ geotrans: num(0)
  .. .. ..@ transfun:function ()
  ..@ ncols   : int 2116
  ..@ nrows   : int 1780
  ..@ crs     :Formal class 'CRS' [package "sp"] with 1 slots
  .. .. ..@ projargs: chr "+proj=utm +zone=32 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0"
  ..@ z       : list()

Von: Robert J. Hijmans [mailto:r.hijmans@gmail.com<mailto:r.hijmans@gmail.com>]
Gesendet: Donnerstag, 31. Januar 2013 17:34
An: Reichmuth, Anne

Betreff: Re: [R-sig-Geo] probability predict by class

Anne, Please you try this 'fun' argument instead. Robert

fun=function(x, ...){predict(x, ...)$posterior}
On Thu, Jan 31, 2013 at 5:03 AM, <Anne.Reichmuth@dlr.de<mailto:Anne.Reichmuth@dlr.de>> wrote:
Hi Robert,
thank you for your reply.
Rprob is a prediction of raster and not lda. Just that there is no confusion.

I tried your suggestion with:

Rprob <- predict(D,lda.0x,fun=function(x){predict(x)$posterior}
but it always gives me the error :
"Fehler in fun(model, blockvals, ...) :
  unbenutzte(s) Argument(e) (blockvals)" What is blockvals? I could not find any helpful information.

I tried to replace x with many different things but I havent found the right one. What do I have to set x to?
The structure of lda.0x is as follows:

List of 8
$ prior  : Named num [1:3] 0.221 0.61 0.17
  ..- attr(*, "names")= chr [1:3] "Bu" "Fi" "Ta"
$ counts : Named int [1:3] 203 561 156<tel:3%5D%20203%20561%20156>
  ..- attr(*, "names")= chr [1:3] "Bu" "Fi" "Ta"
$ means  : num [1:3, 1:4] 0.39727 -0.14565 0.00682 0.30567 -0.08872 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:3] "Bu" "Fi" "Ta"
  .. ..$ : chr [1:4] "X0.6258" "X0.6548" "X0.7309" "X0.8686"
$ scaling: num [1:4, 1:2] 1.48 -2.34 -4.59 6.74 9.11 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:4] "X0.6258" "X0.6548" "X0.7309" "X0.8686"
  .. ..$ : chr [1:2] "LD1" "LD2"
$ lev    : chr [1:3] "Bu" "Fi" "Ta"
$ svd    : num [1:2] 38.84 6.86
$ N      : int 920
$ call   : language lda(x = data0.x[, c(59, 67, 88, 126)], grouping = grouping.code)
- attr(*, "class")= chr "lda"

Anne

Von: Robert J. Hijmans [mailto:r.hijmans@gmail.com<mailto:r.hijmans@gmail.com>]
Gesendet: Donnerstag, 31. Januar 2013 01:37
An: Reichmuth, Anne
Cc: r-sig-geo@r-project.org<mailto:r-sig-geo@r-project.org>
Betreff: Re: [R-sig-Geo] probability predict by class

Anne,

you do this: Rprob <- predict(D, lda.0x, type="probabilities")

But  ?predict.lda  does not say that you can use an argument like  type="probabilities"

To get the posterior probabilities you could probably do:

Rprob <- predict(D, lda.0x, fun=function(x){predict(x)$posterior})

(based on what ?predict.lda  says under "Value")

Robert

On Wed, Jan 30, 2013 at 6:21 AM, <Anne.Reichmuth@dlr.de<mailto:Anne.Reichmuth@dlr.de>> wrote:
Dear R group,

I have a question about calculating and plotting probability of a classification using a raster image.
My data consist of three different classes, in my case three tree species with specific reference spectra. Linear discriminant analysis was used for classifying the spectra
>library(MASS)
>lda.0x=lda(data0.x[,c(140,62,31,13)],grouping.code)  #data0.x are 4 selected bands and grouping.code are the classes

Results of lda.0x:

Call:
lda(data0.x[, c(140, 62, 31, 13)], grouping = grouping.code)

Prior probabilities of groups:
       Bu        Fi        Ta
0.2206522 0.6097826 0.1695652

Group means:
      X0.9193      X0.6367     X0.5244     X0.4591
Bu  1.2526457  0.375728279  0.47791591  0.45878963
Fi -0.4867618 -0.134819712 -0.19422522 -0.14486541
Ta  0.1204249 -0.004096041  0.07656038 -0.07605643

Coefficients of linear discriminants:
               LD1        LD2
X0.9193  3.0803877  0.7704369
X0.6367 -1.1332850  4.6852711
X0.5244 -1.2421211 -7.7201729
X0.4591  0.5219285  2.4969266

Proportion of trace:
   LD1    LD2
0.9664 0.0336

What I now want is to calculate the probability of these classes using the whole image
>Rprob <- predict(D, lda.0x, type="probabilities") #D is image where the reference spectra have been collected before

class       : RasterLayer
dimensions  : 1780, 2116, 3766480  (nrow, ncol, ncell)
resolution  : 2, 2  (x, y)
extent      : 771779, 776011, 5305525, 5309085  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=32 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : in memory
names       : layer
values      : 1, 3  (min, max)


First problem: no probability was calculated
Second problem: I would like to separate the predict classification by class so that I can plot the probability by class.

It would be terrific if someone might know how to reach that goal. I also tried this using ksvm which brings me to the same problem of selecting by class.

Greetings, Anne



        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo@r-project.org<mailto:R-sig-Geo@r-project.org>
https://stat.ethz.ch/mailman/listinfo/r-sig-geo




	[[alternative HTML version deleted]]

