INTEGER() error

184 views
Skip to first unread message

Anirban Basu

unread,
Aug 4, 2021, 8:02:36 PM8/4/21
to R-inla discussion group
Hi all,

 I am trying to run a simple model:
formula <- airqlty ~ 1 + f(ID, model="bym", graph = W, hyper = 
                             list(prec.unstruct = list(prior="loggamma", param=c(1,0.01)),
                                  prec.spatial = list(prior="loggamma", param=c(1,0.001 ))))

mod<-inla(formula, family="gaussian", data=leafdata)

But it gives me an error:
Error in card(neighbours) : 
  INTEGER() can only be applied to a 'integer', not a 'character'

My graph file is fine, with 3105 neighbors and corresponding weights. The neighbors are identified with corrresponding integer values. Also, ID in my dataset is an integer.  I just dont know where INTEGER() is being applied. Any help is appreciated.

Thanks,
Anirban

Helpdesk

unread,
Aug 5, 2021, 2:15:28 AM8/5/21
to Anirban Basu, R-inla discussion group

I think you have a graph that is of wrong type, ie your 'W' is not a
'inla.graph' class or a numerical matrix
> --
> You received this message because you are subscribed to the Google
> Groups "R-inla discussion group" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to r-inla-discussion...@googlegroups.com.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/r-inla-discussion-group/c7b31677-1ff9-4f0f-aaca-5c571f760735n%40googlegroups.com
> .

--
Håvard Rue
he...@r-inla.org

Anirban Basu

unread,
Aug 5, 2021, 12:51:00 PM8/5/21
to R-inla discussion group
I created W in this way. If you have any reco as to how to differently create W, I would appreciate it.

Thanks,

us.poly<-readOGR("cb_2016_us_county_500k.shp")
# Remove counties with no neighbours
list.queen<-poly2nb(us.poly, queen=TRUE)
cards<-card(list.queen)
tr<- which(cards==0)
sub.poly<- us.poly[-tr, ]

# Merge spatial df with downloaded data.
leafmap2 <- merge(sub.poly, county_dat, by=c("GEOID"), all=FALSE)
list.queen2<-poly2nb(leafmap2, queen=TRUE)

# create inla graph
set.ZeroPolicyOption(TRUE)
W<-nb2listw(list.queen2, style="W", zero.policy=TRUE)
W

W has three elements, Style (==W), neighbours, and weights

> card(W$neighbours)
   [1]  6  6  7  5  7  7  4  6  6  4  9  7  5  4  6  7  6  7  5  6  6  7  5  6  5  7
  [27]  6  9  6  7  6  7  6  5  6  6  9  7  5  6  7  6  5  5  5  8  4  6  4  6  5  3
  [53]  5  6  6  6  7  7  5  6  5  6  4  5  5  8  4  6  4  4  9  7  2 10  7  5  4  4
  [79]  6  6  6  5  3  9  4  8  7  4  5  7  5  7  8  6  6  5  9  6  4  8  7  5  8  9
 [105]  3  6  6  6  4  5  6  6  9  7  6  7  5  6  6  6  7  5  6  5  7  4  4  5  7  7
 [131]  6  4  7  6  7  4  6  6  6  7  6  5  7  6  7  6  6  8  6  8  7  6  5  8  6  6
 [157]  6  5  8  6  6  7  6  5  8  6  7  6  5  5  4  4  6  5  4  5  6  4  6  8  1  3
 [183]  4  9  3  3  4  6  6  8  1  6  3  2  6  6  9  6  5  6  8  7  6  8  7  6  5  6
 [209]  7  8  6  5  8  6  7  7  5  6  9  3  4  6  6  5  5  6  4  5  5  6  7  6  3  6
 [235]  5  4  6  8  8  3  6  6  2  5  6  7  1  5  5  7  6  6  5  4  3  6  5  3  8  7
 [261]  8  5  5  3  5  5  7  5  8  6  3  6  6  8  8  5  4  4  4  5  5  7  6  6  8  6
 [287]  4  5  8  5  8  7  7  5  5  5  7  1  6  5  7  6  4  4  7  7  5  6  6  7  7  5
 [313]  5  7  6  7  6 10  5  5  6  5  5  7  6  7  4  7  5  5  6  7  3  5  3  5  5  7
 [339]  6  4  7  6  6  7  6  6  8  7  6  6  6  7  6  7  5  7  7  7  6  6  6  5  6 11
 [365]  6  6  5  4  7  2  8  6  6  8  5  2  7  4  5  4  8  5  5  5  7  4  4  6  8  7
 [391]  6  7  6  6  6  6  4  1  7  7  6  4  6  7  6  6  6  6  6  6  4  8  6  2  4  9
 [417]  4  4  8  8  3  6  6  5  7  3  6  6  6  3  6  6  6  6  6  7  6  6  7  6  6  7
 [443]  4  5  7  5  5  6  7  6  7  4  7  3  6  6  4  4  4  5  6  5  7  7  3  3  5  4
 [469]  3  7  4  2  8  6  5  6  6  7  9  5  5  5  6  5  5  7  5  5  4  4  3  3  6  3
 [495]  6  7  7  7  4  6  7  8  5  5  7  7  6 10  4  4  6  7  7  6  7  5  7  7  6  6
 [521]  5  4  6 13  7  9  6  7  4  6  4  3  5  4  3  6  5  6  6  4  9  5  8  5  8  6
 [547]  6  6  7  4  6  5  6  5  6  6  6  6  5  7  6  6  6  6  7  6  5  5  6  5  6  7
 [573]  7  5  6  6  7  5  7  6  6  6  7  7  7  6  6  6  6  6  5  6  6  5  4  7  5  4
 [599]  7  6  3  6  5  6  7  7  6  1  3  4  6  7  6  5  7  6  4  6  8  9  7  5  7  5
 [625]  6  6  6  7  6  8  6  7  5  8  6  6  6  7  6  7  6  7  6  5  7  4  8  8  3  5
 [651]  7  4  5  5  6  7  6  4  7  5  6  6  6  7  5  6  8  7  6  4  3  7  6  9  6  6
 [677]  7  7  6  8  7  6  6  7  4  4  4  6  4  6  7  7  6  4  5  4  7  8  5  6  5  6
 [703]  4  6  5  3  7  6  4  2  5  5  4  6  6  5  6  7  7  7  2  7  7  5  7  6  4  6
 [729]  7  8  7  4  5  4  5  5  6  6  5  6  8  5  8  5  8  6  4  6  7  7  6  6  6  6
 [755]  6  7  8  6  8  5  4  5  7  9  6  6  9  7  6  7  5  5  6  6  5  8  7  7  7  4
 [781]  6  8  9  6  5  9  5  6  6  7  6  6  7  8  6  7  6  5  6  7  5  6  5  4  6  7
 [807]  8  6  6  7  6  5  6  8  8  7  5  4  7  5  5  5  6  5  9  8  6  6  6  8  5  6
 [833]  7  5  7  7  5  5  6  5  6  5  5  6  7  8  6  7  5  5  4  5  5  5  7  6  4  6
 [859]  6  6  5  8  5  7  4  5  5  8  5  4  6  6  4  4  4  4  8  3  4  5  3  7  7  4
 [885]  9  7  6  7  8  2  2  3  6  2 10  6  5  8  7  7  8  5  5  7  5  4  7  7  6  7
 [911]  9  2  7  8  8  6  4  5  6  5  6  7  6  7  7  6  4  4  5  5  4  7  5  6  5  6
 [937]  7  4  8  8  8  6  6  6  5  6  6  5  5  4  6  6  5  6  8  7  6  7  3  7  8  8
 [963]  8  7  6  4  9  7  7  5  4  5  2  8  6  5  5  2  6  8  7  6  4  7  7  6  7  7
 [989]  3  5  7  7  6  5  7  7  7  5  7  5
 [ reached getOption("max.print") -- omitted 2104 entries ]


Helpdesk

unread,
Aug 5, 2021, 12:59:27 PM8/5/21
to Anirban Basu, R-inla discussion group

I don't think 'nb2listw' gives a valid representation.

nb2INLA ?

Virgilio Gómez-Rubio

unread,
Aug 5, 2021, 1:26:34 PM8/5/21
to Helpdesk, Anirban Basu, R-inla discussion group
Hi,

‘nb2mat’ will take an ‘nb’ object and create an adjacency matrix that INLA can take. ‘listw2mat’ will create a weight matrix from a list object. Also, you can convert these matrices into sparse matrices with as(matrix, “Matrix”), for example.

Best,

Virgilio

Anirban Basu

unread,
Aug 5, 2021, 2:03:20 PM8/5/21
to R-inla discussion group
Thanks a lot. It seems to work if I do:

W<-nb2listw(list.queen2, style="W", zero.policy=TRUE)
WW<-listw2mat(W)

and use WW instead of W.  

But it creates a new problem when running inla which says :
" Error in inla(formula, family = "gaussian", data = leafdata) : f(GEOID). Covariate does not match 'values' 3037 times.", which is strange because my WW is of 3015x3015 dimension, and my data leafdata  also have 3015 rows. 

Anirban

Anirban Basu

unread,
Aug 5, 2021, 2:31:58 PM8/5/21
to R-inla discussion group
Ok, I have fixed that problem by creating a new ID that just runs from 1 to NROW(data).

A

Reply all
Reply to author
Forward
0 new messages