coef_transform: Transform linear model coefficients to original feature space

Description Usage Arguments Value Examples

View source: R/coef_transform.R

Description

Transforms linear model coefficients from a model fitted using latent features z back to the original feature space x. This is possible since the dimension reduction methods PCA, SPCA and ISPCA are all linear, and consequently, if a linear model is fitted using features z, it will correspond exactly to a linear model with features x. This function can be used to compute the regression coefficients and intercept of that model.

Usage

1
2
## S3 method for class 'dimred'
coef_transform(object, beta, alpha, ...)

Arguments

object

Dimension reduction object.

beta

Regression coefficients in the z-space (latent space).

alpha

Intercept in the z-space (latent space).

...

Currently ignored.

Value

A list with elements beta and alpha that give the model's regression coefficients and intercept, respectively, in the original feature space.

Examples

 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
###

# load data
data("ovarian", package = "dimreduce")
x <- ovarian$x
y <- ovarian$y

# perform dimension reduction (use first two latent features)
dr <- ispca(x, y, nctot = 2, normalize = TRUE)
z <- predict(dr, x)

# fit model
data <- data.frame(z, y)
if (requireNamespace("rstanarm", quietly = TRUE)) {

  # Bayesian logistic regression
  model <- rstanarm::stan_glm(y ~ ., data = data, family = binomial(), chains = 2, iter = 500)
  param_z <- as.data.frame(model)
  alpha_z <- t(param_z[, 1])
  beta_z <- t(param_z[, 2:3])

  # transform the linear model back to original feature space
  param_x <- coef_transform(dr, beta_z, alpha_z)
  beta_x <- param_x$beta
  alpha_x <- param_x$alpha

  intervals <- apply(beta_x, 1, quantile, probs = c(0.05, 0.5, 0.95))

  if (require("ggplot2", quietly = TRUE)) {
    # plot the coefficient posteriors in the original space
    p <- ncol(x)
    ggplot() +
      geom_linerange(aes(x = 1:p, ymin = intervals[1, ], ymax = intervals[3, ])) +
      geom_point(aes(x = 1:p, y = intervals[2, ])) +
      ylab("Coefficient") +
      xlab("Feature")
  }
} else {

  # logistic regression
  model <- glm(y ~ ., data = data, family = binomial())
  param_z <- coef(model)
  alpha_z <- param_z[1]
  beta_z <- param_z[2:3]

  # transform the linear model back to original feature space
  param_x <- coef_transform(dr, beta_z, alpha_z)
  beta_x <- param_x$beta
  alpha_x <- param_x$alpha

  if (requireNamespace("ggplot2", quietly = TRUE)) {
    # plot the coefficients in the original space
    p <- ncol(x)
    ggplot2::ggplot() +
      ggplot2::geom_point(ggplot2::aes(x = 1:p, y = beta_x)) +
      ggplot2::ylab("Coefficient") +
      ggplot2::xlab("Feature")
  }
}

jpiironen/dimreduce documentation built on March 18, 2021, 11:52 p.m.