File: methods_vgam.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 (91 lines) | stat: -rw-r--r-- 2,288 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
# classes: .vglm, .vgam


########### .vgam ---------------


#' @export
model_parameters.vgam <- model_parameters.gam


#' @export
standard_error.vgam <- function(model, ...) {
  params <- insight::get_parameters(model)
  se <- sqrt(diag(insight::get_varcov(model)))
  # sort
  se <- se[params$Parameter]
  .data_frame(
    Parameter = .remove_backticks_from_string(names(se)),
    SE = as.vector(se),
    Component = params$Component
  )
}


#' @export
p_value.vgam <- function(model, ...) {
  stat <- insight::get_statistic(model)
  stat$p <- as.vector(stats::pchisq(stat$Statistic, df = insight::get_df(model), lower.tail = FALSE))

  stat[c("Parameter", "p", "Component")]
}


#' @export
simulate_model.vgam <- function(model, iterations = 1000, ...) {
  out <- .simulate_model(model, iterations, component = "all")
  class(out) <- c("parameters_simulate_model", class(out))
  out
}


########### .vglm ---------------


#' @export
p_value.vglm <- function(model, ...) {
  insight::check_if_installed("VGAM")

  cs <- VGAM::summary(model)@coef3
  p <- cs[, 4]

  .data_frame(
    Parameter = .remove_backticks_from_string(names(p)),
    p = as.vector(p)
  )
}


#' @export
standard_error.vglm <- function(model, ...) {
  se <- sqrt(diag(insight::get_varcov(model)))
  .data_frame(
    Parameter = .remove_backticks_from_string(names(se)),
    SE = as.vector(se)
  )
}


# ci.vgam <- function(x, ci = 0.95, component = c("all", "conditional", "smooth"), ...) {
#   component <- match.arg(component)
#
#   # dof and SE
#   dof <- degrees_of_freedom(x)
#   se <- standard_error(x)$SE
#   params <- insight::get_parameters(x)
#
#   se <- se[!is.na(dof)]
#   dof <- dof[!is.na(dof)]
#   params_names <- names(dof)
#
#   # Wald CI for non-chisq parameters
#   out <- .ci_generic(model = x, ci = ci, dof = Inf)
#
#   chisq_fac <- stats::qchisq(se, df = dof, lower.tail = FALSE)
#   for (i in 1:length(params_names)) {
#     out$CI_low[out$Parameter == params_names[i]] <- params$Estimate[params$Parameter == params_names[i]] - se[i] * chisq_fac[i]
#     out$CI_high[out$Parameter == params_names[i]] <- params$Estimate[params$Parameter == params_names[i]] + se[i] * chisq_fac[i]
#   }
#
#   out
# }