[R] FW: problem with markov random field smooths in mgcv
Wilcox, Chris (O&A, Hobart)
Chr|@@W||cox @end|ng |rom c@|ro@@u
Wed Mar 18 08:44:53 CET 2020
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)))
More information about the R-help
mailing list