Validating a combined ZAP GAM

43 views
Skip to first unread message

Sophie Loca

unread,
Nov 16, 2021, 5:16:31 AM11/16/21
to R-inla discussion group
Hi folks,

I'm trying to run a two part ZAP GAM on r-inla, one bernoulli part (with the smoother) and a zero truncated poisson part (no smoother). I am having trouble combining the two parts, the code for this i am taking from the second volume of 'Beginners guide to Spatial, Temporal and Spatial-Temporal Ecological Data Analysis with R-INLA'. 

The covariate matrix was taken from the Bernoulli part of the model to avoid having components of different lengths (page 391, vl 2).

Covariates <- data.frame(
  Intercept   = rep(1, N),
  bath1  = basis_bath[,"bath1"],
  bath2  = basis_bath[,"bath2"],
  bath3  = basis_bath[,"bath3"],
  bath4  = basis_bath[,"bath4"],
  bath5  = basis_bath[,"bath5"],
  bath6  = basis_bath[,"bath6"],
  bath7  = basis_bath[,"bath7"],
  bath8  = basis_bath[,"bath8"],
  bath9  = basis_bath[,"bath9"],
  dco1  = basis_dco[,"dco1"],
  dco2  = basis_dco[,"dco2"],
  dco3  = basis_dco[,"dco3"],
  dco4  = basis_dco[,"dco4"],
  dco5  = basis_dco[,"dco5"],
  dco6  = basis_dco[,"dco6"],
  dco7  = basis_dco[,"dco7"],
  dco8  = basis_dco[,"dco8"],
  dco9  = basis_dco[,"dco9"],
  bath.std = MyStd(Skate$bath),
  dcoast.std = MyStd(Skate$dcoast),
  btemp.std = MyStd(Skate$btemp),
  gravel.std = MyStd(Skate$gravel_sq),
  current.std = MyStd(Skate$current),
  sal.std = MyStd(Skate$sal_sq),
  mud.std = MyStd(Skate$mud_sq)
)

So after running each model, validating and completing model selection, I have tried to run the following code:


# Bernoulli part
X                           <- as.matrix(Covariates)
Beta01                 <- I.01$summary.fixed[,"mean"]
Pi                          <- exp(X %*% Beta01) / (1 + exp(X %*% Beta01))


#Count part
betaTP                <- I.pos$summary.fixed[1:N,"mean"]
mu                       <- exp(X %*% betaTP)
mu.ZTruncPois <-  mu / (1 - exp(-mu))

The problem is that when i try to run the lines with the %*% in it, i get an error saying:
"Error in X %*% Beta01 : non-conformable arguments". The same happens with the line in the count part.

Can anyone give any pointers as to how i could fix this problem? I have a working understanding of the code but the underlying programming logic is still something i am new to. 

Thanks,
Soph



high...@highstat.com

unread,
Nov 17, 2021, 2:07:19 AM11/17/21
to R-inla discussion group
Sophie,

That means that the number of columns in X does not match the number of regression parameters in Beta01 and betaTP. So I guess that your 'Covariates' thing is not good defined.
Just type:
dim(X)  and length(Beta01) and length(betaTP) and you will see what the problem is.

If you do X %*% b,  then the number of columns in X needs to match the number of elements in b. Highschool maths..:-).

Alain

Sophie

unread,
Nov 17, 2021, 5:02:04 AM11/17/21
to R-inla discussion group
Hi Alain,

Thank you for the quick reply, i managed to figure this out yesterday evening. When i read the book i misunderstood it as meaning the covariate matrix would have to be the same length for both parts of the model in order for it to work - silly me. 

Thanks a million,
Sophie
Reply all
Reply to author
Forward
0 new messages