#joint modelling
#importing district level data on crime and corresponding shape file of the district
NE <- read_excel("D:/New_Research/NE_data.xlsx")
crime.shapeNE <- readOGR(dsn="D:/New_research", layer="NE")
merged <- merge(crime.shapeNE, NE, by='censuscode')
nb=poly2nb(crime.shapeNE)# neighbourhood matrix
head(nb)
nb2INLA("map.adj", nb)
g <- inla.read.graph(filename = "map.adj")
merged$re_u <- 1:nrow(merged@data)
#preparing state level data on crime and importing the shape file of the state
k=crime.shapeNE_states <- readOGR(dsn="D:/New_research", layer="NE_State")
ST_CD=c(18,12,13,14,15,16,17)
FemPop=c(17011147, 736799, 1049838, 1560229, 595855, 1979496, 1623063)
crime1.num=c(26352, 281, 39, 247, 172, 874, 568)
NE_state=data.frame(ST_CD, FemPop, crime1.num)
R1=sum(NE_state$crime1.num)/sum(NE_state$FemPop)
NE_state$Ecrime1=R1*NE_state$FemPop
merged1 <- merge(k, NE_state, by='ST_CD')
#preparing data for joint modelling
m=nrow(merged1@data)
s=nrow(merged@data)
l=m+s
Y=Matrix(NA, ncol = 2, nrow = l)
Y1=merged1$crime1.num
Y2=merged$crime.num.y
Y[1:m,1]=Y1
Y[8:l,2]=Y2
#intercept
Inter=matrix(NA, nrow = l, ncol = 2)
Inter[1:m,1]=1
Inter[8:l,2]=1
#offset
E1=merged1$Ecrime1
E2=merged$Ecrime
E=c(E1, E2)
ID.shared1=c(as.numeric(merged1$ST_CD),rep(NA, s))
ID.shared2=c(rep(NA, m),as.numeric(merged$ST_CEN_CD.x))
ID2=c(rep(NA,7), merged$re_u)
# model
form1=Y~0+Inter+ f(ID.shared1, model="bym", graph=g1) + f(ID.shared2, copy= 'ID.shared1', model="bym",graph=g1) + f(ID2, model="bym", graph=g)
fit=inla(form1, family = c("poisson", "poisson"), data = list(Y=Y, Inter=Inter, E=E, ID.shared1=ID.shared1, ID.shared2=ID.shared2, ID2=ID2), E=E, control.compute = list(waic=T), control.predictor = list(compute = TRUE))
what is the mistake I have done here.
Error in inla.core(formula = formula, family = family, contrasts = contrasts, :
f(ID.shared1). Covariate does not match 'values' 7 times. Indexes for mismatch:1 2 3 4 5 6 7. This is not what you want. Use NA values in the covariate!
*** inla.core.safe: inla.program has crashed: rerun to get better initial values. try=1/2
Error in inla.core(formula = formula, family = family, contrasts = contrasts, :
f(ID.shared1). Covariate does not match 'values' 7 times. Indexes for mismatch:1 2 3 4 5 6 7. This is not what you want. Use NA values in the covariate!
*** inla.core.safe: inla.program has crashed: rerun to get better initial values. try=2/2
Error in inla.core(formula = formula, family = family, contrasts = contrasts, :
f(ID.shared1). Covariate does not match 'values' 7 times. Indexes for mismatch:1 2 3 4 5 6 7. This is not what you want. Use NA values in the covariate!
Error in inla.core.safe(formula = formula, family = family, contrasts = contrasts, :
*** Fail to get good enough initial values. Maybe it is due to something else.