Skip to content

Commit

Permalink
Merge pull request #61 from SoerenPannier/bug/archiving_mumin
Browse files Browse the repository at this point in the history
Bug/archiving mumin
  • Loading branch information
SoerenPannier committed Jun 11, 2024
2 parents 37bf5ff + 8374ba3 commit 08a5af3
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 13 deletions.
3 changes: 1 addition & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,11 @@ URL: https://github.com/SoerenPannier/emdi
LazyData: true
Encoding: UTF-8
Copyright: inst/COPYRIGHTS
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
Imports:
nlme,
moments,
ggplot2,
MuMIn,
gridExtra,
openxlsx,
reshape2,
Expand Down
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ export(write.excel)
export(write.ods)
importFrom(HLMdiag,mdffits)
importFrom(MASS,ginv)
importFrom(MuMIn,r.squaredGLMM)
importFrom(boot,boot)
importFrom(formula.tools,"lhs<-")
importFrom(formula.tools,lhs)
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# emdi 2.2.2
* Addition of logit transformation in FH model
* Removal of MuMIn dependency


# emdi 2.2.1
* Substitution of maptools and rgeos by sf due to depreciation of the former
* Substitution of ggplot2::fortify by ggplot2::geom_sf due to depreciation of the former
Expand Down
3 changes: 0 additions & 3 deletions R/summary.direct.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
# Summarizes an emdi Direct Object

#' @export
#' @importFrom moments skewness kurtosis
#' @importFrom MuMIn r.squaredGLMM
#' @rdname emdi_summaries

summary.direct <- function(object, ...) {
throw_class_error(object, "direct")

Expand Down
41 changes: 39 additions & 2 deletions R/summary.ebp.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#' @export
#' @importFrom moments skewness kurtosis
#' @importFrom MuMIn r.squaredGLMM
#' @rdname emdi_summaries

summary.ebp <- function(object, ...) {
Expand Down Expand Up @@ -105,7 +104,7 @@ summary.ebp <- function(object, ...) {
)
tempMod <- object$model
tempMod$call$fixed <- object$fixed
r_squared <- suppressWarnings(r.squaredGLMM(tempMod))
r_squared <-lme_rsquared(tempMod)
if (is.matrix(r_squared)) {
r_marginal <- r_squared[1, 1]
r_conditional <- r_squared[1, 2]
Expand Down Expand Up @@ -188,3 +187,41 @@ icc <- function(model) {
e <- model$sigma^2
u / (u + e)
}

# Conditional and marginal R square
# Code for conditional and marginal R square is an adapted copy from the MuMIn R Package Version: 1.47.5 under the GPL-2 License

lme_rsquared <- function(x) {

VarFx <- var(fitted(x, level = 0L))
mmRE <- model.matrix(x$modelStruct$reStruct,
data = x$data[rownames(x$fitted), ,
drop = FALSE])
n <- nrow(mmRE)
sigma2 <- x$sigma^2
reStruct <- x$modelStruct$reStruct
if ((m <- length(reStruct)) > 1L) {
nams <- names(reStruct)
for (i in seq.int(m)) attr(reStruct[[i]], "Dimnames")[[2L]] <- paste(nams[[i]],
attr(reStruct[[i]], "Dimnames")[[2L]], sep = ".")
}
varRe <- sum(vapply(reStruct, function(z) {
sig <- nlme::pdMatrix(z) * sigma2
mm1 <- mmRE[, rownames(sig), drop = FALSE]
sum(matmultdiag(mm1 %*% sig, ty = mm1)) / n
}, FUN.VALUE = numeric(1)))
varTot <- sum(VarFx, varRe)
res <- c(VarFx, varTot)/(varTot + sigma2)
names(res) <- c("R2m", "R2c")

return(res)
}

matmultdiag <- function (x, y, ty = t(y))
{
if (ncol(x) != ncol(ty))
stop("non-conformable arguments")
if (nrow(x) != nrow(ty))
stop("result is not a square matrix")
return(rowSums(x * ty))
}
3 changes: 1 addition & 2 deletions R/summary.emdi.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
#' R2 from generalized linear mixed-effects models. Methods in Ecology and
#' Evolution, 4(2), 133-142.
#' @seealso \code{\link{emdiObject}}, \code{\link{direct}}, \code{\link{ebp}},
#' \code{\link{fh}}, \code{\link[MuMIn]{r.squaredGLMM}},
#' \code{\link[moments]{skewness}},
#' \code{\link{fh}}, \code{\link[moments]{skewness}},
#' \code{\link[moments]{kurtosis}}, \code{\link[stats]{shapiro.test}}
#' @examples
#' \donttest{
Expand Down
1 change: 0 additions & 1 deletion R/summary.fh.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#' @export
#' @importFrom moments skewness kurtosis
#' @importFrom MuMIn r.squaredGLMM
#' @rdname emdi_summaries

summary.fh <- function(object, ...) {
Expand Down
25 changes: 25 additions & 0 deletions man/emdi.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions man/emdi_summaries.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 08a5af3

Please sign in to comment.