leifeld / btergm

Temporal Exponential Random Graph Models by Bootstrapped Pseudolikelihood
16 stars 10 forks source link

Error in gof for model with geometrically weighted statistics #12

Closed lucas-dopaes closed 5 years ago

lucas-dopaes commented 5 years ago

I have fitted the btergm model model "prob3c<-btergm(net.list ~ edges+ dgwesp(0, fixed = TRUE, type="ITP"),+ dgwesp(0, fixed = TRUE) + dgwdsp(0, fixed = TRUE)+gwodegree(0, fixed = TRUE) +memory(type = "stability"), R = 1000)". There, net.list is a list of 65 network objects, with varying sizes of node sets, from 58 to 170.

The model runs well and gives a reasonable output in summary(prob3c). However, I'm not being able to further check goodness-of-fit and degeneracy for this model.

I get the following warning and error when I try to run gof3c <- gof(prob3c, statistics = c( geodesic, odeg, ideg)): "There was a problem with the simulation at t = 50. Sometimes this may be due to node attributes being incomplete at some time steps. For example, not all levels of a nodefactor term may be present at all time steps. Original error message: Error in weights(object): argument "object" is missing, with no default."

I read through the coding of the gof function (at https://rdrr.io/cran/btergm/src/R/gofmethods.R), but I can't quite identify the source of the error.

Given the error message and as "prob3c" is a btegm model with geometrically weighted statistics, I wonder if that error is related to the decay parameter that the curved exponential family of statistics have. Are you aware of any intrinsic incompatibility between the gof function and these statistics?

Thank you all very much for your attention and help on this.

leifeld commented 5 years ago

Hi Lucas,

Geometrically weighted statistics having an additional parameter if their alpha parameter is not fixed (i.e., the parameter for the statistic and an additional parameter for the alpha value). If the alpha value is fixed, there is only a single parameter for the statistic. The gof function employs the simulate function to simulate new networks to compare with the observed network. The simulate function is not able to simulate networks when the alpha is not fixed. As a consequence, GOF assessment in btergm only works if the alpha value is fixed. The error message should not appear if that is the case. To verify this, see the example below:

library("btergm")
library("network")
set.seed(5)

networks <- list()
for(i in 1:10) {           # create 10 random networks with 10 actors
  mat <- matrix(rbinom(100, 1, .25), nrow = 10, ncol = 10)
  diag(mat) <- 0           # loops are excluded
  nw <- network(mat)       # create network object
  networks[[i]] <- nw      # add network to the list
}

covariates <- list()
for (i in 1:10) {          # create 10 matrices as covariate
  mat <- matrix(rnorm(100), nrow = 10, ncol = 10)
  covariates[[i]] <- mat   # add matrix to the list
}

fit <- btergm(networks ~ edges + gwesp(0.5, fixed = TRUE) + edgecov(covariates), R = 100)
summary(fit)

# works
g <- gof(fit, statistics = c(geodesic, odeg, ideg))
plot(g)

fit2 <- btergm(networks ~ edges + gwesp(fixed = FALSE) + edgecov(covariates), R = 100)

# does not work
g2 <- gof(fit2, statistics = c(geodesic, odeg, ideg))

What I don't understand now is why your code with the fixed alpha values produces this error message anyway. Can you perhaps provide a minimal self-contained example I can look at? Thanks!

Also make sure you are using the most recent btergm version from GitHub.

lucas-dopaes commented 5 years ago

Dear Dr Leifeld,

Thank you for your prompt and careful explanation. I've run the verification code you provided and the gof function did not work even when the parameters where fixed. I then uploaded again the btergm package and it all worked well, both for your code and for mine. As you anticipated, the problem was in fact only that my version of btergm was not the most recent one.

I thought that this was unlikely, as I reinstalled xergm last week, but it does seem that I had an outdated version of btergm. Sorry about not testing this possibility before posting the issue here and thank you once again for your help.

Best regards, Lucas

PS: Good news: the model appears to be a good fit after all.