IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

R Discussion :

GAM tensor product


Sujet :

R

  1. #1
    Membre actif Avatar de habasque
    Homme Profil pro
    Ingénieur d'études
    Inscrit en
    Septembre 2006
    Messages
    530
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Finistère (Bretagne)

    Informations professionnelles :
    Activité : Ingénieur d'études
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Septembre 2006
    Messages : 530
    Points : 296
    Points
    296
    Par défaut GAM tensor product
    Bonjour,

    Je teste actuellement un GAM avec la formule suivante :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    gam(presence_absence~ te(SST, log(CHLA)),data=dataset,family="binomial")
    En revanche, l'interprétation du graphique résultat du modèle est assez complexe...

    Si quelqu'un a l'habitude de ce genre de GAM, je suis preneur !

    Merci d'avance,
    Images attachées Images attachées  

  2. #2
    Membre actif Avatar de habasque
    Homme Profil pro
    Ingénieur d'études
    Inscrit en
    Septembre 2006
    Messages
    530
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Finistère (Bretagne)

    Informations professionnelles :
    Activité : Ingénieur d'études
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Septembre 2006
    Messages : 530
    Points : 296
    Points
    296
    Par défaut vis.gam
    Bonjour,

    Petit update de ce post.
    La solution trouvée pour l'interprétation du modèle a été de passer par une visualisation en 2D par l'utilisation de l'option 'plot.type="contour"' de la fonction vis.gam du package mgcv :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    vis.gam(model,view=c("CHLA","SST"),xlim=c(0,15),ylim=c(10,30),xlab="CHL-a (mg.m-3)",ylab="SST (°C)",
            plot.type="contour",type="response",color="terrain",n.grid=200)
    Maintenant je souhaiterais spécifier les couleurs utilisées avec la palette suivante :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    # colors palette definition
    jet.colors <-colorRampPalette(c("#00007F", "blue", "#007FFF", "cyan",
                                    "#7FFF7F", "yellow", "#FF7F00", "red", "#7F0000"))
    vis.gam(model,view=c("CHLA","SST"),xlim=c(0,15),ylim=c(10,30),xlab="CHL-a (mg.m-3)",ylab="SST (°C)",
            plot.type="contour",type="response",color=jet.colors(10),n.grid=200)
    Mais le message d'erreur suivant apparaît :
    Error in vis.gam(model, view = c("CHLA", "SST"), xlim = c(0, 15), ylim = c(10, :
    color scheme not recognised
    Si quelqu'un a une solution, je suis preneur !
    Merci d'avance,

  3. #3
    Membre actif Avatar de habasque
    Homme Profil pro
    Ingénieur d'études
    Inscrit en
    Septembre 2006
    Messages
    530
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Finistère (Bretagne)

    Informations professionnelles :
    Activité : Ingénieur d'études
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Septembre 2006
    Messages : 530
    Points : 296
    Points
    296
    Par défaut
    En customisant la fonction vis.gam() cela fonctionne :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    jet.colors <-colorRampPalette(c("#00007F", "blue", "#007FFF", "cyan",
                                    "#7FFF7F", "yellow", "#FF7F00", "red", "#7F0000"))
     
    myvis.gam <- function (x, view = NULL, cond = list(), n.grid = 30, too.far = 0, 
              col = NA, color = "heat", contour.col = NULL, se = -1, type = "link", 
              plot.type = "persp", zlim = NULL, nCol = 50, ...) 
    {
      fac.seq <- function(fac, n.grid) {
        fn <- length(levels(fac))
        gn <- n.grid
        if (fn > gn) 
          mf <- factor(levels(fac))[1:gn]
        else {
          ln <- floor(gn/fn)
          mf <- rep(levels(fac)[fn], gn)
          mf[1:(ln * fn)] <- rep(levels(fac), rep(ln, fn))
          mf <- factor(mf, levels = levels(fac))
        }
        mf
      }
      dnm <- names(list(...))
      v.names <- names(x$var.summary)
      if (is.null(view)) {
        k <- 0
        view <- rep("", 2)
        for (i in 1:length(v.names)) {
          ok <- TRUE
          if (is.matrix(x$var.summary[[i]])) 
            ok <- FALSE
          else if (is.factor(x$var.summary[[i]])) {
            if (length(levels(x$var.summary[[i]])) <= 1) 
              ok <- FALSE
          }
          else {
            if (length(unique(x$var.summary[[i]])) == 1) 
              ok <- FALSE
          }
          if (ok) {
            k <- k + 1
            view[k] <- v.names[i]
          }
          if (k == 2) 
            break
        }
        if (k < 2) 
          stop("Model does not seem to have enough terms to do anything useful")
      }
      else {
        if (sum(view %in% v.names) != 2) 
          stop(paste(c("view variables must be one of", v.names), 
                     collapse = ", "))
        for (i in 1:2) if (!inherits(x$var.summary[[view[i]]], 
                                     c("numeric", "factor"))) 
          stop("Don't know what to do with parametric terms that are not simple numeric or factor variables")
      }
      ok <- TRUE
      for (i in 1:2) if (is.factor(x$var.summary[[view[i]]])) {
        if (length(levels(x$var.summary[[view[i]]])) <= 1) 
          ok <- FALSE
      }
      else {
        if (length(unique(x$var.summary[[view[i]]])) <= 1) 
          ok <- FALSE
      }
      if (!ok) 
        stop(paste("View variables must contain more than one value. view = c(", 
                   view[1], ",", view[2], ").", sep = ""))
      if (is.factor(x$var.summary[[view[1]]])) 
        m1 <- fac.seq(x$var.summary[[view[1]]], n.grid)
      else {
        r1 <- range(x$var.summary[[view[1]]])
        m1 <- seq(r1[1], r1[2], length = n.grid)
      }
      if (is.factor(x$var.summary[[view[2]]])) 
        m2 <- fac.seq(x$var.summary[[view[2]]], n.grid)
      else {
        r2 <- range(x$var.summary[[view[2]]])
        m2 <- seq(r2[1], r2[2], length = n.grid)
      }
      v1 <- rep(m1, n.grid)
      v2 <- rep(m2, rep(n.grid, n.grid))
      newd <- data.frame(matrix(0, n.grid * n.grid, 0))
      for (i in 1:length(x$var.summary)) {
        ma <- cond[[v.names[i]]]
        if (is.null(ma)) {
          ma <- x$var.summary[[i]]
          if (is.numeric(ma)) 
            ma <- ma[2]
        }
        if (is.matrix(x$var.summary[[i]])) 
          newd[[i]] <- matrix(ma, n.grid * n.grid, ncol(x$var.summary[[i]]), 
                              byrow = TRUE)
        else newd[[i]] <- rep(ma, n.grid * n.grid)
      }
      names(newd) <- v.names
      newd[[view[1]]] <- v1
      newd[[view[2]]] <- v2
      if (type == "link") 
        zlab <- paste("linear predictor")
      else if (type == "response") 
        zlab <- type
      else stop("type must be \"link\" or \"response\"")
      fv <- predict.gam(x, newdata = newd, se.fit = TRUE, type = type)
      z <- fv$fit
      if (too.far > 0) {
        ex.tf <- exclude.too.far(v1, v2, x$model[, view[1]], 
                                 x$model[, view[2]], dist = too.far)
        fv$se.fit[ex.tf] <- fv$fit[ex.tf] <- NA
      }
      if (is.factor(m1)) {
        m1 <- as.numeric(m1)
        m1 <- seq(min(m1) - 0.5, max(m1) + 0.5, length = n.grid)
      }
      if (is.factor(m2)) {
        m2 <- as.numeric(m2)
        m2 <- seq(min(m1) - 0.5, max(m2) + 0.5, length = n.grid)
      }
      if (se <= 0) {
        old.warn <- options(warn = -1)
        av <- matrix(c(0.5, 0.5, rep(0, n.grid - 1)), n.grid, 
                     n.grid - 1)
        options(old.warn)
        max.z <- max(z, na.rm = TRUE)
        z[is.na(z)] <- max.z * 10000
        z <- matrix(z, n.grid, n.grid)
        surf.col <- t(av) %*% z %*% av
        surf.col[surf.col > max.z * 2] <- NA
        if (!is.null(zlim)) {
          if (length(zlim) != 2 || zlim[1] >= zlim[2]) 
            stop("Something wrong with zlim")
          min.z <- zlim[1]
          max.z <- zlim[2]
        }
        else {
          min.z <- min(fv$fit, na.rm = TRUE)
          max.z <- max(fv$fit, na.rm = TRUE)
        }
        surf.col <- surf.col - min.z
        surf.col <- surf.col/(max.z - min.z)
        surf.col <- round(surf.col * nCol)
        con.col <- 1
        if (color == "heat") {
          pal <- heat.colors(nCol)
          con.col <- 3
        }
        else if (color == "topo") {
          pal <- topo.colors(nCol)
          con.col <- 2
        }
        else if (color == "cm") {
          pal <- cm.colors(nCol)
          con.col <- 1
        }
        else if (color == "terrain") {
          pal <- terrain.colors(nCol)
          con.col <- 2
        }
        else if (color == "gray" || color == "bw") {
          pal <- gray(seq(0.1, 0.9, length = nCol))
          con.col <- 1
        }
        ### customized here
        else if (color == 'jet') {
          pal <- jet.colors(nCol)
          con.col = 1
        }
        ####
        else stop("color scheme not recognised")
        if (is.null(contour.col)) 
          contour.col <- con.col
        surf.col[surf.col < 1] <- 1
        surf.col[surf.col > nCol] <- nCol
        if (is.na(col)) 
          col <- pal[as.array(surf.col)]
        z <- matrix(fv$fit, n.grid, n.grid)
        if (plot.type == "contour") {
          stub <- paste(ifelse("xlab" %in% dnm, "", ",xlab=view[1]"), 
                        ifelse("ylab" %in% dnm, "", ",ylab=view[2]"), 
                        ifelse("main" %in% dnm, "", ",main=zlab"), ",...)", 
                        sep = "")
          if (color != "bw") {
            txt <- paste("image(m1,m2,z,col=pal,zlim=c(min.z,max.z)", 
                         stub, sep = "")
            eval(parse(text = txt))
            txt <- paste("contour(m1,m2,z,col=contour.col,zlim=c(min.z,max.z)", 
                         ifelse("add" %in% dnm, "", ",add=TRUE"), ",...)", 
                         sep = "")
            eval(parse(text = txt))
          }
          else {
            txt <- paste("contour(m1,m2,z,col=1,zlim=c(min.z,max.z)", 
                         stub, sep = "")
            eval(parse(text = txt))
          }
        }
        else {
          stub <- paste(ifelse("xlab" %in% dnm, "", ",xlab=view[1]"), 
                        ifelse("ylab" %in% dnm, "", ",ylab=view[2]"), 
                        ifelse("main" %in% dnm, "", ",zlab=zlab"), ",...)", 
                        sep = "")
          if (color == "bw") {
            op <- par(bg = "white")
            txt <- paste("persp(m1,m2,z,col=\"white\",zlim=c(min.z,max.z) ", 
                         stub, sep = "")
            eval(parse(text = txt))
            par(op)
          }
          else {
            txt <- paste("persp(m1,m2,z,col=col,zlim=c(min.z,max.z)", 
                         stub, sep = "")
            eval(parse(text = txt))
          }
        }
      }
      else {
        if (color == "bw" || color == "gray") {
          subs <- paste("grey are +/-", se, "s.e.")
          lo.col <- "gray"
          hi.col <- "gray"
        }
        else {
          subs <- paste("red/green are +/-", se, "s.e.")
          lo.col <- "green"
          hi.col <- "red"
        }
        if (!is.null(zlim)) {
          if (length(zlim) != 2 || zlim[1] >= zlim[2]) 
            stop("Something wrong with zlim")
          min.z <- zlim[1]
          max.z <- zlim[2]
        }
        else {
          z.max <- max(fv$fit + fv$se.fit * se, na.rm = TRUE)
          z.min <- min(fv$fit - fv$se.fit * se, na.rm = TRUE)
        }
        zlim <- c(z.min, z.max)
        z <- fv$fit - fv$se.fit * se
        z <- matrix(z, n.grid, n.grid)
        if (plot.type == "contour") 
          warning("sorry no option for contouring with errors: try plot.gam")
        stub <- paste(ifelse("xlab" %in% dnm, "", ",xlab=view[1]"), 
                      ifelse("ylab" %in% dnm, "", ",ylab=view[2]"), ifelse("zlab" %in% 
                                                                             dnm, "", ",zlab=zlab"), ifelse("sub" %in% dnm, 
                                                                                                            "", ",sub=subs"), ",...)", sep = "")
        txt <- paste("persp(m1,m2,z,col=col,zlim=zlim", ifelse("border" %in% 
                                                                 dnm, "", ",border=lo.col"), stub, sep = "")
        eval(parse(text = txt))
        par(new = TRUE)
        z <- fv$fit
        z <- matrix(z, n.grid, n.grid)
        txt <- paste("persp(m1,m2,z,col=col,zlim=zlim", ifelse("border" %in% 
                                                                 dnm, "", ",border=\"black\""), stub, sep = "")
        eval(parse(text = txt))
        par(new = TRUE)
        z <- fv$fit + se * fv$se.fit
        z <- matrix(z, n.grid, n.grid)
        txt <- paste("persp(m1,m2,z,col=col,zlim=zlim", ifelse("border" %in% 
                                                                 dnm, "", ",border=hi.col"), stub, sep = "")
        eval(parse(text = txt))
      }
    }
    puis en l'appelant comme suit :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    vis.gam(model,view=c("CHLA","SST"),xlim=c(0,15),ylim=c(10,30),xlab="CHL-a (mg.m-3)",ylab="SST (°C)",
            plot.type="contour",type="response",color="terrain",n.grid=200)

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [INFO] Gestion d'un environnement TEST / PRODUCTION
    Par nuke_y dans le forum Oracle
    Réponses: 19
    Dernier message: 05/08/2005, 16h14
  2. Tests Unitaires - Production de documents
    Par giviz dans le forum Test
    Réponses: 13
    Dernier message: 07/02/2005, 08h41
  3. Choix d'un sgbd open source pour de la production
    Par gueeyom dans le forum Décisions SGBD
    Réponses: 5
    Dernier message: 14/05/2004, 11h40

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo