Skip to content
Permalink
Browse files

Made compatible with new tibble

New tibble allows named columns hence comparisons with unnamed vectors failed. Changed apply to purrr::map_dbl to fix. Also changed the variance calculations to be shorter and more efficient.
  • Loading branch information
khvorov45 committed Mar 1, 2020
1 parent 00c99c6 commit fa696d94a42add43287de2198e1873542dffa28d
Showing with 19 additions and 12 deletions.
  1. +3 −2 DESCRIPTION
  2. +1 −0 NAMESPACE
  3. +15 −10 R/methods.R
@@ -1,6 +1,6 @@
Package: sclr
Title: Scaled Logistic Regression
Version: 0.3.0.9000
Version: 0.3.1
Authors@R:
person(given = "Arseniy",
family = "Khvorov",
@@ -20,7 +20,8 @@ Imports:
tibble,
dplyr,
rlang,
stats
stats,
purrr
Suggests:
knitr,
rmarkdown,
@@ -27,6 +27,7 @@ importFrom(dplyr,inner_join)
importFrom(dplyr,mutate)
importFrom(dplyr,pull)
importFrom(dplyr,select)
importFrom(purrr,map_dbl)
importFrom(rlang,":=")
importFrom(rlang,.data)
importFrom(rlang,abort)
@@ -118,6 +118,7 @@ logLik.sclr <- function(object, ...) {
#' @importFrom stats predict delete.response model.frame model.matrix qnorm
#' @importFrom dplyr bind_cols
#' @importFrom tibble tibble
#' @importFrom purrr map_dbl
#'
#' @export
predict.sclr <- function(object, newdata, ci_lvl = 0.95, ...) {
@@ -134,18 +135,22 @@ predict.sclr <- function(object, newdata, ci_lvl = 0.95, ...) {
ests_beta_mat <- matrix(ests[-1], ncol = 1) # Estimated betas

# Point estimates
prot_point_lin <- apply(model_mat, 1, function(x) x %*% ests_beta_mat)
prot_point_lin <- map_dbl(
1:nrow(model_mat),
function(i) matrix(model_mat[i, ], nrow = 1) %*% ests_beta_mat
)

# Modified beta covariance matrices
# Variance of linear predictor
ests_beta_cov <- vcov(object)[-1, -1] # Beta covariances
object_coefs <- get_x_coeffs(model_mat) # object modifiers
cov_mod_mats <- build_symm_mat(object_coefs) # In a list of matrices
cov_modified <- lapply(cov_mod_mats, function(x) x * ests_beta_cov)

# Standard deviations associated with each of the point estimates
sds <- lapply(cov_modified, sum)
sds <- unlist(sds)
sds <- sqrt(sds)
lin_vars <- map_dbl(
1:nrow(model_mat),
function(i) {
matrix(model_mat[i, ], nrow = 1) %*%
ests_beta_cov %*%
matrix(model_mat[i, ], ncol = 1)
}
)
sds <- sqrt(lin_vars)

# Ranges
lvl <- qnorm((1 + ci_lvl) / 2)

0 comments on commit fa696d9

Please sign in to comment.
You can’t perform that action at this time.