File: extract_random_parameters.R

package info (click to toggle)
r-cran-parameters 0.24.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,852 kB
  • sloc: sh: 16; makefile: 2
file content (124 lines) | stat: -rw-r--r-- 3,842 bytes parent folder | download
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
.extract_random_parameters <- function(model, ...) {
  UseMethod(".extract_random_parameters")
}


.extract_random_parameters.merMod <- function(model,
                                              ci = 0.95,
                                              effects = "random",
                                              ...) {
  insight::check_if_installed("lme4")

  out <- as.data.frame(lme4::ranef(model, condVar = TRUE), stringsAsFactors = FALSE)
  colnames(out) <- c("Group", "Parameter", "Level", "Coefficient", "SE")

  # coerce to character
  out$Parameter <- as.character(out$Parameter)
  out$Level <- as.character(out$Level)
  out$Group <- as.character(out$Group)
  out$Effects <- "random"

  if (length(ci) == 1) {
    fac <- stats::qnorm((1 + ci) / 2)
    out$CI_low <- out$Coefficient - fac * out$SE
    out$CI_high <- out$Coefficient + fac * out$SE
    ci_cols <- c("CI_low", "CI_high")
  } else {
    ci_cols <- NULL
    for (i in ci) {
      fac <- stats::qnorm((1 + i) / 2)
      ci_low <- paste0("CI_low_", i)
      ci_high <- paste0("CI_high_", i)
      out[[ci_low]] <- out$Coefficient - fac * out$SE
      out[[ci_high]] <- out$Coefficient + fac * out$SE
      ci_cols <- c(ci_cols, ci_low, ci_high)
    }
  }

  stat_column <- gsub("-statistic", "", insight::find_statistic(model), fixed = TRUE)

  # to match rbind
  out[[stat_column]] <- NA
  out$df_error <- NA
  out$p <- NA

  out <- out[c("Parameter", "Level", "Coefficient", "SE", ci_cols, stat_column, "df_error", "p", "Effects", "Group")]

  if (effects == "random") {
    out[c(stat_column, "df_error", "p")] <- NULL
  }

  out
}


.extract_random_parameters.glmmTMB <- function(model,
                                               ci = 0.95,
                                               effects = "random",
                                               component = "conditional",
                                               ...) {
  insight::check_if_installed("lme4")
  out <- as.data.frame(lme4::ranef(model, condVar = TRUE))
  colnames(out) <- c("Component", "Group", "Parameter", "Level", "Coefficient", "SE")

  # filter component
  out <- switch(component,
    zi = ,
    zero_inflated = out[out$Component == "zi", ],
    cond = ,
    conditional = out[out$Component == "cond", ],
    disp = ,
    dispersion = out[out$Component == "disp", ],
    out
  )

  # coerce to character
  out$Parameter <- as.character(out$Parameter)
  out$Level <- as.character(out$Level)
  out$Group <- as.character(out$Group)
  out$Effects <- "random"

  # rename
  out$Component[out$Component == "zi"] <- "zero_inflated"
  out$Component[out$Component == "cond"] <- "conditional"
  out$Component[out$Component == "disp"] <- "dispersion"

  if (length(ci) == 1) {
    fac <- stats::qnorm((1 + ci) / 2)
    out$CI_low <- out$Coefficient - fac * out$SE
    out$CI_high <- out$Coefficient + fac * out$SE
    ci_cols <- c("CI_low", "CI_high")
  } else {
    ci_cols <- NULL
    for (i in ci) {
      fac <- stats::qnorm((1 + i) / 2)
      ci_low <- paste0("CI_low_", i)
      ci_high <- paste0("CI_high_", i)
      out[[ci_low]] <- out$Coefficient - fac * out$SE
      out[[ci_high]] <- out$Coefficient + fac * out$SE
      ci_cols <- c(ci_cols, ci_low, ci_high)
    }
  }

  stat_column <- gsub("-statistic", "", insight::find_statistic(model), fixed = TRUE)

  # to match rbind
  out[[stat_column]] <- NA
  out$df_error <- NA
  out$p <- NA

  out <- out[c(
    "Parameter", "Level", "Coefficient", "SE", ci_cols, stat_column,
    "df_error", "p", "Component", "Effects", "Group"
  )]

  if (effects == "random") {
    out[c(stat_column, "df_error", "p")] <- NULL
  }
  out
}


.extract_random_parameters.MixMod <- function(model, ...) {
  NULL
}