Description Usage Arguments Details Value Author(s) See Also Examples
View source: R/calclogLfunctions.R
Calculates the conditional log-likelihood for a set of parameter estimates from a fitted model, where everything is treated as "fixed effects" including latent variables, row effects, and so on. WARNING: As of version 1.9, this function is no longer being maintained (and probably does not work properly, if at all)!
1 2 3 |
y |
The response matrix the model was fitted to. |
X |
The covariate matrix used in the model. Defaults to |
family |
Either a single element, or a vector of length equal to the number of columns in the response matrix. The former assumes all columns of the response matrix come from this distribution. The latter option allows for different distributions for each column of the response matrix. Elements can be one of "binomial" (with probit link), "poisson" (with log link), "negative.binomial" (with log link), "normal" (with identity link), "lnormal" for lognormal (with log link), "tweedie" (with log link), "exponential" (with log link), "gamma" (with log link), "beta" (with logit link), "ordinal" (cumulative probit regression). Please see |
trial.size |
Either equal to a single element, or a vector of length equal to the number of columns in y. If a single element, then all columns assumed to be binomially distributed will have trial size set to this. If a vector, different trial sizes are allowed in each column of y. The argument is ignored for all columns not assumed to be binomially distributed. Defaults to 1, i.e. Bernoulli distribution. |
lv.coefs |
The response-specific intercept, coefficient estimates relating to the latent variables, and dispersion parameters from the fitted model. |
X.coefs |
The coefficients estimates relating to the covariate matrix from the fitted model. Defaults to |
row.coefs |
Row effect estimates for the fitted model. The conditional likelihood is defined conditional on these estimates i.e., they are also treated as “fixed effects". Defaults to |
row.ids |
A matrix with the number of rows equal to the number of rows in the response matrix, and the number of columns equal to the number of row effects to be included in the model. Element (i,j) indicates the cluster ID of row i in the response matrix for random effect eqnj; please see the |
offset |
A matrix with the same dimensions as the response matrix, specifying an a-priori known component to be included in the linear predictor during fitting. Defaults to |
lv |
Latent variables "estimates" from the fitted model, which the conditional likelihood is based on. Defaults to |
cutoffs |
Common cutoff estimates from the fitted model when any of the columns of the response matrix are ordinal responses. Defaults to |
powerparam |
Common power parameter from the fitted model when any of the columns of the response matrix are tweedie responses. Defaults to |
For an n x p response matrix \bm{Y}, suppose we fit a model with one or more latent variables. If we denote the latent variables by \bm{u}_i; i = 1,…,n, then the conditional log-likelihood is given by,
\log(f) = ∑_{i=1}^n ∑_{j=1}^p \log \{f(y_{ij} | \bm{u}_i, \bm{θ}_j, β_{0j}, …)\},
where f(y_{ij}|\cdot) is the assumed distribution for column j, \bm{u}_i are the latent variables and \bm{θ}_j are the coefficients relating to them, β_{0j} are response-specific intercepts, and … denotes anything else included in the model, such as row effects, regression coefficients related the covariate matrix and the trait matrix, etc...
The key difference between this and the marginal likelihood (see calc.marglogLik
) is that the conditional likelihood treats everything as "fixed effects" i.e., conditions on them. These include the latent variables \bm{u}_i and other parameters that were included in the model as random effects e.g., row effects if row.eff = "random"
, regression coefficients related to the covariate matrix if traits were included in the model, and so on.
The conditional DIC, WAIC, EAIC, and EBIC returned from get.measures
are based on the conditional likelihood calculated from this function. Additionally, get.measures
returns the conditional likelihood evaluated at all MCMC samples of a fitted model.
A list with the following components:
logLik |
Value of the conditional log-likelihood. |
logLik.comp |
A matrix of the log-likelihood values for each element in the response matrix, |
Francis K.C. Hui [aut, cre], Wade Blanchard [aut]
Maintainer: Francis K.C. Hui <fhui28@gmail.com>
calc.logLik.lv0
to calculate the conditional/marginal log-likelihood for a model with no latent variables; calc.marglogLik
for calculation of the marginal log-likelihood;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | ## Not run:
## NOTE: The values below MUST NOT be used in a real application;
## they are only used here to make the examples run quick!!!
example_mcmc_control <- list(n.burnin = 10, n.iteration = 100,
n.thin = 1)
testpath <- file.path(tempdir(), "jagsboralmodel.txt")
library(mvabund) ## Load a dataset from the mvabund package
data(spider)
y <- spider$abun
n <- nrow(y)
p <- ncol(y)
## Example 1 - model with 2 latent variables, site effects,
## and no environmental covariates
spiderfit_nb <- boral(y, family = "negative.binomial",
lv.control = list(num.lv = 2), row.eff = "fixed",
save.model = TRUE, mcmc.control = example_mcmc_control,
model.name = testpath)
## Extract all MCMC samples
fit_mcmc <- get.mcmcsamples(spiderfit_nb)
mcmc_names <- colnames(fit_mcmc)
## Find the posterior medians
coef_mat <- matrix(apply(fit_mcmc[,grep("lv.coefs",mcmc_names)],
2,median),nrow=p)
site_coef <- list(ID1 = apply(fit_mcmc[,grep("row.coefs.ID1", mcmc_names)],
2,median))
lvs_mat <- matrix(apply(fit_mcmc[,grep("lvs",mcmc_names)],2,median),nrow=n)
## Calculate the conditional log-likelihood at the posterior median
calc.condlogLik(y, family = "negative.binomial",
lv.coefs = coef_mat, row.coefs = site_coef, lv = lvs_mat)
## Example 2 - model with no latent variables and environmental covariates
X <- scale(spider$x)
spiderfit_nb2 <- boral(y, X = X, family = "negative.binomial",
save.model = TRUE, mcmc.control = example_mcmc_control,
model.name = testpath)
## Extract all MCMC samples
fit_mcmc <- get.mcmcsamples(spiderfit_nb2)
mcmc_names <- colnames(fit_mcmc)
## Find the posterior medians
coef_mat <- matrix(apply(fit_mcmc[,grep("lv.coefs",mcmc_names)],
2,median),nrow=p)
X_coef_mat <- matrix(apply(fit_mcmc[,grep("X.coefs",mcmc_names)],
2,median),nrow=p)
## Calculate the log-likelihood at the posterior median
calc.condlogLik(y, X = X, family = "negative.binomial",
lv.coefs = coef_mat, X.coefs = X_coef_mat)
## End(Not run)
|
Loading required package: coda
This is boral version 1.9. If you recently updated boral, please check news(package = "boral") for the updates in the latest version.
row.ids assumed to be a matrix with one column and elements 1,2,...nrow(y) i.e., a row-specific intercept.
module glm loaded
Compiling model graph
Resolving undeclared variables
Allocating nodes
Graph information:
Observed stochastic nodes: 336
Unobserved stochastic nodes: 466
Total graph size: 2570
Initializing model
$logLik
[1] -864.2524
$logLik.comp
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] -5.8645902 -2.9184783 -0.2045214 -0.428773936 -0.13407908 -3.14787832
[2,] -0.6277877 -1.7283992 -0.1879003 -0.195223901 -0.12169937 -7.84400701
[3,] -4.7290543 -3.9135856 -3.0227310 -2.935789741 -0.14654812 -3.75811679
[4,] -2.7076244 -2.4607552 -0.3717294 -2.389489998 -0.16252525 -4.78560761
[5,] -2.1911477 -3.8019941 -0.2772902 -2.933354662 -0.15245541 -3.79129703
[6,] -0.7691070 -2.4668204 -0.2042483 -4.023932683 -0.12934705 -3.43340383
[7,] -2.6150706 -2.6533824 -0.2526324 -5.333836254 -0.13990068 -4.42652552
[8,] -0.7503560 -3.1091797 -0.2283818 -0.228752250 -0.13888147 -3.50387089
[9,] -2.1176777 -1.1806798 -0.6717432 -0.062342408 -0.18826056 -0.26788387
[10,] -3.1866370 -0.1169972 -1.9332994 -0.003146907 -0.13977888 -0.02255054
[11,] -4.9581522 -1.1917624 -2.5560159 -0.046798085 -0.18093643 -1.95551912
[12,] -4.6672757 -3.4502056 -0.4954044 -0.241058296 -0.18386607 -1.06486907
[13,] -2.9496062 -4.5990573 -2.5798448 -2.974232101 -0.11355539 -4.55140187
[14,] -0.6917474 -1.7741999 -0.2281291 -2.298750108 -0.13116226 -2.95498674
[15,] -0.5320944 -1.1905857 -0.1822434 -0.101779731 -0.11416498 -0.53294916
[16,] -0.4693112 -1.9376821 -0.1081698 -0.081017318 -0.09731741 -0.60513030
[17,] -0.8167608 -1.3338181 -0.5629038 -0.101064364 -0.17990973 -0.46957359
[18,] -0.5224004 -1.2060715 -0.2950936 -0.029298240 -0.13121336 -0.21235637
[19,] -0.5844895 -1.2209547 -0.2725013 -0.062163680 -0.13275893 -0.40262104
[20,] -0.6725181 -1.6349416 -0.2984055 -0.107742176 -0.14230051 -0.60742736
[21,] -0.6612936 -1.6137277 -0.2930905 -0.072919331 -0.14279914 -0.55121134
[22,] -3.9144204 -0.1526105 -5.6021049 -0.136364525 -4.69087277 -0.18058233
[23,] -4.7224003 -0.6605106 -4.5631959 -0.086907827 -5.23350828 -0.25854471
[24,] -4.4375664 -0.2749553 -5.4114808 -0.041331985 -4.90134361 -0.11812949
[25,] -4.0528696 -1.1851109 -4.4134804 -0.152984798 -0.18509578 -2.45391177
[26,] -3.0180755 -0.5449790 -3.6133073 -0.068687817 -6.20616080 -0.20863922
[27,] -5.5025168 -0.3684977 -4.2038354 -0.070055092 -4.68788506 -0.15890043
[28,] -4.5531172 -1.2372149 -4.5627397 -0.092916165 -3.35555172 -0.38487009
[,7] [,8] [,9] [,10] [,11] [,12]
[1,] -0.5817892 -9.9617781 -4.8962328 -7.49695955 -7.0754294 -3.1655145
[2,] -2.1755065 -2.5883594 -5.6754130 -7.88004417 -8.1291783 -4.0209581
[3,] -2.2984995 -6.7227436 -5.3810451 -7.39023760 -7.1495013 -2.2841430
[4,] -2.2124031 -4.6286373 -5.8877200 -8.49429540 -7.6720578 -4.9841855
[5,] -2.3027943 -3.2533268 -9.6638210 -8.15862073 -7.5967017 -4.5240130
[6,] -0.4635718 -5.7216771 -6.0279834 -6.58107567 -7.4811851 -5.8325226
[7,] -2.1969751 -7.6035648 -9.1952852 -9.47740918 -8.8432574 -4.5607424
[8,] -7.5874818 -3.2519041 -3.0519770 -3.18476688 -6.1710506 -2.9012868
[9,] -0.5156175 -6.3320503 -2.4136100 -3.17447381 -3.3249358 -0.9168054
[10,] -0.2996725 -7.1770711 -0.1595542 -0.09014833 -2.6613993 -0.4101614
[11,] -0.5058928 -10.0520176 -0.3577022 -3.17198729 -4.0002974 -0.8609582
[12,] -0.7466260 -8.5766356 -2.5020129 -5.23945533 -5.1657328 -1.2348774
[13,] -2.2982849 -6.8528565 -7.1675071 -8.15414676 -7.6850217 -4.8721927
[14,] -3.1780790 -5.9185625 -5.4468819 -9.21632254 -8.8805625 -6.2859474
[15,] -4.1901557 -0.2454708 -0.3078753 -0.14662274 -6.5886269 -2.8238695
[16,] -4.0540505 -0.2466814 -3.0970231 -0.13919142 -6.7675388 -3.1667621
[17,] -2.7951990 -0.3708560 -0.4135693 -0.18732560 -5.9706203 -2.5651455
[18,] -3.8961848 -0.2791079 -0.2614029 -0.13133103 -6.9021989 -0.6397129
[19,] -5.1686356 -0.2928933 -2.3888090 -0.15013325 -6.2356197 -2.8184484
[20,] -5.0215087 -0.3198101 -0.3662637 -0.17090078 -5.9851685 -2.5019410
[21,] -5.1888955 -2.6191629 -0.3497698 -3.17217339 -5.6817018 -2.4950596
[22,] -0.1977827 -3.3422975 -0.3541807 -0.15157276 -2.6501743 -0.7339835
[23,] -2.7858612 -4.3311080 -0.4573100 -0.20027140 -2.7180370 -1.0734406
[24,] -0.3847837 -3.6516245 -0.3463780 -0.15808348 -0.2951566 -0.8014332
[25,] -2.1756430 -5.1317281 -4.1570029 -0.19164559 -5.5286265 -3.4811383
[26,] -0.5188300 -0.3998158 -0.4318907 -0.19109459 -2.7052397 -1.0151610
[27,] -0.4785914 -2.6584444 -0.4440252 -0.19337514 -0.3520099 -1.0276794
[28,] -0.6533121 -4.3295776 -0.4731979 -0.20946933 -3.3612729 -1.1384162
Warning message:
`calc.marglogLik()` is deprecated as of boral 1.9.
We will be phasing out all functions to calculate log-likelihoods of any sort (too hard to maintain)!
This warning is displayed once every 8 hours.
Call `lifecycle::last_warnings()` to see where this warning was generated.
Compiling model graph
Resolving undeclared variables
Allocating nodes
Graph information:
Observed stochastic nodes: 336
Unobserved stochastic nodes: 432
Total graph size: 2675
Initializing model
$logLik
[1] -842.8227
$logLik.comp
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] -4.34028409 -3.79705733 -1.02381008 -0.195589627 -0.07405879 -3.68240131
[2,] -0.88937878 -2.70309046 -0.41106701 -0.444095308 -0.06349083 -5.66443516
[3,] -3.72032534 -4.61292988 -2.39128298 -2.882469584 -0.05754348 -4.52289044
[4,] -2.17281303 -3.35208823 -0.73563008 -2.038346703 -0.05254339 -5.65985216
[5,] -1.28353819 -4.43689569 -0.55904511 -2.677646946 -0.01656254 -4.54502982
[6,] -0.41625912 -3.30755223 -0.09667719 -3.878729409 -0.34915039 -4.06602215
[7,] -1.77302581 -3.69642252 -0.45103845 -4.609824004 -0.02116195 -5.02691685
[8,] -0.40170895 -5.07559453 -0.38725030 -0.051275823 -0.13396051 -4.47455038
[9,] -2.43082126 -2.35869217 -0.64079451 -0.153269820 -0.07118027 -0.23707453
[10,] -2.42262044 -0.63258744 -1.79893816 -0.329771666 -0.40214777 -0.09599993
[11,] -3.53136900 -2.06917784 -2.39442179 -0.117949092 -0.08383418 -2.32635946
[12,] -3.60426994 -4.10458397 -0.91868249 -0.160736783 -0.08283653 -0.33176842
[13,] -2.11289905 -5.48663727 -1.81541595 -2.699825988 -0.18684880 -5.24574763
[14,] -0.86378389 -3.12891483 -0.33216295 -2.265845274 -0.02997776 -3.81710658
[15,] -0.19394149 -1.40335751 -0.15641745 -0.113021238 -0.09874558 -0.97155568
[16,] -0.04048717 -2.59181572 -0.08076245 -0.013185704 -0.05132215 -0.80025583
[17,] -0.23125847 -1.44822226 -0.16280636 -0.102978372 -0.10267085 -0.92490995
[18,] -0.21097588 -1.91925005 -0.23423140 -0.069141458 -0.08533247 -0.99629667
[19,] -0.09994028 -1.89271962 -0.11712338 -0.026286113 -0.09279960 -0.82609114
[20,] -0.03846729 -2.31204765 -0.07122350 -0.015665820 -0.04988792 -0.79530815
[21,] -0.07887965 -1.84554007 -0.12759330 -0.030472087 -0.07417180 -0.92900557
[22,] -2.84286312 -0.14590005 -4.92616580 -0.007568936 -4.05024450 -0.02011615
[23,] -3.68653103 -0.15378945 -4.28157260 -0.045818003 -4.59233577 -0.11686659
[24,] -3.25902071 -0.34303726 -5.08608494 -0.105197123 -4.25569319 -0.09001955
[25,] -3.75747048 -1.82809029 -3.90310051 -0.414035972 -0.30661393 -3.00824839
[26,] -2.35477123 -0.02943623 -3.49281286 -0.014286128 -5.62123951 -0.07606962
[27,] -4.17381769 -0.11159672 -4.04709897 -0.027885991 -4.04339532 -0.20691458
[28,] -3.59469861 -0.07936037 -4.23009433 -0.047650018 -2.80104399 -0.16585540
[,7] [,8] [,9] [,10] [,11] [,12]
[1,] -0.5240773 -7.3240529 -4.7950785 -7.69986379 -7.9588688 -3.1115286
[2,] -2.1913848 -2.2677679 -4.9796788 -8.55540927 -8.4586316 -3.9041457
[3,] -1.8583483 -5.8486594 -5.2375675 -7.75271570 -8.1337523 -2.1772049
[4,] -1.9275016 -3.9678814 -5.8543836 -9.23431623 -9.2756876 -5.3052716
[5,] -2.0398500 -2.8326248 -8.4588995 -9.50928995 -8.1796790 -4.5253513
[6,] -1.4422052 -4.7299428 -5.9717209 -6.52006752 -8.8454775 -6.6561295
[7,] -2.1194141 -5.9885861 -7.5404575 -9.90339605 -9.3112171 -4.4685365
[8,] -7.3497674 -2.8181435 -3.0709956 -3.41341790 -6.7695841 -2.8426219
[9,] -0.6657621 -5.4777989 -2.4070881 -3.35423464 -3.3411052 -0.7470784
[10,] -0.4880063 -5.6536663 -0.3859906 -0.35325550 -2.7668093 -0.4940356
[11,] -0.5255577 -8.2506288 -0.4928486 -3.34581616 -4.0279587 -0.7074701
[12,] -0.5497247 -8.4716899 -2.4404053 -5.40508514 -5.4062836 -0.7765612
[13,] -2.1680769 -6.8614060 -6.6670965 -7.95364894 -8.2270636 -4.7797951
[14,] -2.9608954 -4.7071873 -4.9950100 -8.51877462 -10.2966615 -5.5049604
[15,] -3.5974268 -0.6265912 -0.5979367 -0.08260335 -6.2543410 -2.9665051
[16,] -3.5652052 -0.7248906 -2.9998872 -0.08248410 -6.1706195 -3.1123286
[17,] -2.8163992 -0.6049695 -0.6004603 -0.08264873 -6.0759211 -2.7180208
[18,] -3.3699510 -0.6201745 -0.6060212 -0.07230861 -6.0845372 -1.1311701
[19,] -4.1554842 -0.7207614 -2.4189311 -0.08839047 -6.0910084 -2.8357678
[20,] -4.2369221 -0.7182766 -0.4639170 -0.08434893 -5.8535612 -2.5104667
[21,] -4.4430138 -2.2940235 -0.5194878 -3.40044562 -5.6914495 -2.5181429
[22,] -0.1906375 -2.9068874 -0.2373307 -0.32237402 -2.7977853 -0.1560836
[23,] -3.4444278 -3.8174703 -0.3711181 -0.29651485 -2.7451300 -0.2449195
[24,] -0.2933844 -3.1743812 -0.3706393 -0.31790665 -0.3843676 -0.3374320
[25,] -1.8258064 -4.4862245 -4.0856572 -0.29572071 -5.4328456 -4.0757319
[26,] -0.1011682 -0.7146069 -0.3002299 -0.33354396 -2.8708063 -0.2139542
[27,] -0.1545259 -2.3775138 -0.3894917 -0.25966353 -0.3632093 -0.2172953
[28,] -0.1527222 -3.8196121 -0.3703957 -0.30291420 -3.3666050 -0.1903298
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.