[R] FW: problem with markov random field smooths in mgcv

Wilcox, Chris (O&A, Hobart) Chr|@@W||cox @end|ng |rom c@|ro@@u
Thu Apr 30 01:44:46 CEST 2020


Thanks very much Simon, that is super helpful.

Best,

Chris

On 25/3/20, 9:47 am, "Simon Wood" <simon.wood using bath.edu> wrote:

    Hi Chris,
    
    It's kind of a documentation glitch, a node is not supposed to be listed 
    as its own neighbour (it causes the diagonal entries in the penalty 
    matrix to be over-written by the wrong value). i.e. the neighbour list 
    should be.
    
      NB <- list()
         NB$'East Timor' <- c(2,15)
         NB$Australia <- c(1,15)
         NB$'Sri Lanka' <-c(12,16)
         NB$Bangladesh <-c(12,13,16)
         NB$Philippines <- c(6,11,14,15,17)
         NB$Taiwan <- c(5,11)
         NB$Thailand <- c(8,10,12,13,14,15)
         NB$Vietnam <- c(7,10,11,14,15)
         NB$`South Korea` <- c(11)
         NB$Cambodia <- c(7,8)
         NB$China <- c(5,6,8,9,14)
         NB$India <- c(3,4,7,13,15,16)
         NB$Myanmar <- c(4,7,12,16)
         NB$Malaysia <- c(5,7,8,11,15)
         NB$Indonesia <- c(1,2,5,7,8,12,14)
         NB$HighSeas2 <- c(3,4,12,13)
         NB$HighSeas1 <- c(5)
    
    I've fixed the help page and had the smooth constructor ignore 
    auto-neighbours for the next release.
    
    best,
    Simon
    
       On 18/03/2020 07:44, Wilcox, Chris (O&A, Hobart) wrote:
    > Hi all,
    >      
    >      I am trying to fit a model with a markov random field smooth in mgcv.  I am having some trouble with getting it to run, and in particular I am getting the message
    >      
    >      Error in initial.sp(w * x, S, off) : S[[1]] matrix is not +ve definite.
    >      
    >      After reading everything I could find on mrf, it sounds like there was a bug that was brought up with Simon Wood in 2012, due to differences between windows and linux, with the linus machine stopping due to this error, while windows was not.  I have not been able to find much else on it.  Any suggestions would be much appreciated.
    >      
    >      There is reproducible code below.
    >      
    >      Thanks
    >      
    >      Chris
    >      
    >      
    >      library(mgcv)
    >      
    >      #create data
    >      Country <- as.factor(c("Australia","Australia","Australia","Australia","Australia","Australia","Bangladesh","Bangladesh","Bangladesh",
    >      "Bangladesh","Bangladesh","Bangladesh","Cambodia","Cambodia","Cambodia","Cambodia","Cambodia","Cambodia",
    >      "China","China","China","China","China","China","East Timor","East Timor","East Timor",
    >      "East Timor","East Timor","East Timor","HighSeas1","HighSeas1","HighSeas1","HighSeas1","HighSeas1","HighSeas1",
    >      "HighSeas2","HighSeas2","HighSeas2","HighSeas2","HighSeas2","HighSeas2","China","China","China","China","China","China",
    >      "India","India","India","India","India","India","Indonesia","Indonesia","Indonesia","Indonesia","Indonesia","Indonesia",
    >      "Malaysia","Malaysia","Malaysia","Malaysia","Malaysia","Malaysia","Myanmar","Myanmar","Myanmar","Myanmar","Myanmar",
    >      "Myanmar","Philippines","Philippines","Philippines","Philippines","Philippines","Philippines","South Korea","South Korea",
    >      "South Korea","South Korea","South Korea","South Korea","China","China","China","China","China","China",
    >      "Sri Lanka","Sri Lanka","Sri Lanka","Sri Lanka","Sri Lanka","Sri Lanka","Taiwan","Taiwan","Taiwan","Taiwan",
    >      "Taiwan","Taiwan","Thailand","Thailand","Thailand","Thailand","Thailand","Thailand","Vietnam","Vietnam","Vietnam","Vietnam",
    >      "Vietnam","Vietnam"))
    >      
    >      Count <- c(0,0,3,5,1,5,0,0,0,0,0,1,0,0,0,0,0,3,0,0,2,1,0,6,0,0,0,1,0,0,0,1,0,0,0,0
    >      ,0,0,20,0,1,0,0,0,0,0,0,2,0,0,6,3,3,10,1,1,18,11,8,11,0,1,2,2,1,14,0,0,0,1,0,0
    >      ,0,0,4,3,9,16,0,0,3,0,0,1,0,0,1,0,0,0,0,0,33,18,8,16,0,0,0,0,0,2,0,1,14,6,8,2
    >      ,0,0,0,0,1,1)
    >      
    >      Data <- data.frame(Count,Country)
    >      
    >      #create neighbour matrix
    >      NB <- list()
    >      NB$'East Timor' <- c(1,2,15)
    >      NB$Australia <- c(1,2,15)
    >      NB$'Sri Lanka' <-c(3,12,16)
    >      NB$Bangladesh <-c(4,12,13,16)
    >      NB$Philippines <- c(5,6,11,14,15,17)
    >      NB$Taiwan <- c(5,6,11)
    >      NB$Thailand <- c(7,8,10,12,13,14,15)
    >      NB$Vietnam <- c(7,8,10,11,14,15)
    >      NB$`South Korea` <- c(9,11)
    >      NB$Cambodia <- c(7,8,10)
    >      NB$China <- c(5,6,8,9,11,14)
    >      NB$India <- c(3,4,7,12,13,15,16)
    >      NB$Myanmar <- c(4,7,12,13,16)
    >      NB$Malaysia <- c(5,7,8,11,14,15)
    >      NB$Indonesia <- c(1,2,5,7,8,12,14,15)
    >      NB$HighSeas2 <- c(3,4,12,13,16)
    >      NB$HighSeas1 <- c(5,17)
    >      
    >      #check levels and names match
    >      all.equal(sort(names(NB)), sort(levels(Data$Country)))
    >      
    >      #try fitting GAM
    >      m1 <- gam(Data$Count ~ s(Data$Country, bs = 'mrf', xt = list(nb = NB)))
    >      
    >      
    >
    > ______________________________________________
    > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
    > https://stat.ethz.ch/mailman/listinfo/r-help
    > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
    > and provide commented, minimal, self-contained, reproducible code.
    
    -- 
    Simon Wood, School of Mathematics, University of Bristol, BS8 1TW UK
    https://people.maths.bris.ac.uk/~sw15190/
    
    



More information about the R-help mailing list