Skip to content

Commit

Permalink
model results as data frame
Browse files Browse the repository at this point in the history
  • Loading branch information
bdilday committed Mar 10, 2018
1 parent 7e67e55 commit 5f776a6
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 1 deletion.
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ export(mnre_dim_and_class_to_index)
export(mnre_expand_matrix)
export(mnre_fit)
export(mnre_fit_sparse)
export(mnre_left_covar_factor)
export(mnre_lk_glm)
export(mnre_lk_penalty)
export(mnre_make_covar)
export(mnre_simulate_ev_data)
export(mnre_simulate_multinomial_data_factors)
export(mnre_step_sparse)
export(nd_min_fun)
import(Matrix)
importFrom(Rcpp,sourceCpp)
Expand Down
4 changes: 4 additions & 0 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ mnre_expand_matrix <- function(x1, k_class, direction) {
.Call('_mnre_mnre_expand_matrix', PACKAGE = 'mnre', x1, k_class, direction)
}

#' @export
mnre_left_covar_factor <- function(x1) {
.Call('_mnre_mnre_left_covar_factor', PACKAGE = 'mnre', x1)
}
Expand All @@ -60,10 +61,12 @@ mnre_fit_sparse <- function(fixed_effects, random_effects, y, theta_mat, Lind, b
.Call('_mnre_mnre_fit_sparse', PACKAGE = 'mnre', fixed_effects, random_effects, y, theta_mat, Lind, beta_fixed, beta_random, verbose)
}

#' @export
mnre_lk_penalty <- function(beta_random, theta_norm, Lind) {
.Call('_mnre_mnre_lk_penalty', PACKAGE = 'mnre', beta_random, theta_norm, Lind)
}

#' @export
mnre_lk_glm <- function(fixed_effects, random_effects, beta_fixed, beta_random, y, Lind) {
.Call('_mnre_mnre_lk_glm', PACKAGE = 'mnre', fixed_effects, random_effects, beta_fixed, beta_random, y, Lind)
}
Expand All @@ -80,6 +83,7 @@ mnre_mu_x <- function(fe_x, re_x, beta_fixed, beta_random) {
.Call('_mnre_mnre_mu_x', PACKAGE = 'mnre', fe_x, re_x, beta_fixed, beta_random)
}

#' @export
mnre_step_sparse <- function(fixed_effects, random_effects, y, beta_fixed, beta_random, lambda_norm, Lind) {
.Call('_mnre_mnre_step_sparse', PACKAGE = 'mnre', fixed_effects, random_effects, y, beta_fixed, beta_random, lambda_norm, Lind)
}
Expand Down
3 changes: 3 additions & 0 deletions R/main.R
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ nd_min_fun <- function(ev) {
ev$verbose <- 1
}

# make sure the data is a data frame, not a tibble
ev$fr <- as.data.frame(ev$fr)

function(mval) {
glf <- lme4::glFormula(ev$frm,
data=ev$fr, family='binomial')
Expand Down
54 changes: 53 additions & 1 deletion R/mnre_fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,56 @@ mnre_fit <- function(frm, data, verbose=0, off_diagonal=0.0) {

ans = optim(mval, nf, method = "L-BFGS", lower=1e-8)

}
mnre_fit_to_df(frm, data, ans$par, verbose = verbose, off_diagonal = off_diagonal)

}

#'
mnre_fit_to_df <- function(frm, data, mval, verbose=0, off_diagonal=0.0) {
data <- as.data.frame(data)
glf <- lme4::glFormula(frm,
data, family='binomial')
fe <- fixed_effects <- (glf$X)
re <- random_effects <- Matrix::t(glf$reTrms$Zt)

y <- matrix(data[,all.vars(frm)[[1]]], ncol=1)
k_class <- max(y)
k <- max(y)
Lind = matrix(glf$reTrms$Lind, ncol=1)

theta_mat <- matrix(mval, ncol=k_class)
covar_mat = mnre_make_covar(theta_mat, Lind, off_diagonal = 0.0)
left_factor <- mnre_left_covar_factor(covar_mat)

fe_sp <- Matrix::Matrix(fe, sparse = TRUE)

beta_re <- matrix(rnorm(ncol(re) * k_class), ncol=k_class)
beta_fe <- matrix(rnorm(ncol(fe) * k_class), ncol=k_class)

zz <- mnre_fit_sparse(fe_sp, re, y, theta_mat, Lind, beta_fe, beta_re, verbose = verbose)

lk1 <- zz$loglk + zz$loglk_det
bpar = matrix(left_factor %*% matrix(zz$beta_random,ncol=1), ncol=k_class)

lvs <- unlist(sapply(glf$reTrms$flist, levels))
cc1 <- as.data.frame(cbind(bpar, Lind=Lind))

ranef_labels <- names(glf$reTrms$cnms)
df_names <- sapply(1:k_class, function(i) {sprintf("class%02d", i)})
df_names <- c(df_names, "Lind")
df_names <- c(df_names, "ranef_label")
df_names <- c(df_names, "ranef_level")

cc1$ranef <- ranef_labels[cc1[,ncol(cc1)]]
cc1$lv <- matrix(lvs, ncol=1)

mvalX <- t(sapply(1:max(Lind), function(i) {
idx = which(cc1[,k_class+1] == i)
tmp <- matrix(bpar[idx,], ncol=k_class)
apply(tmp, 2, sd)
}))

names(cc1) <- df_names
list(ranef=cc1, fixef=zz$beta_fixed, theta=mval)

}
4 changes: 4 additions & 0 deletions src/mnre.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ arma::sp_mat mnre_expand_matrix(const arma::sp_mat& x1, int k_class, int directi
return expanded_mat;
}

//' @export
// [[Rcpp::export]]
arma::sp_mat mnre_left_covar_factor(arma::sp_mat& x1) {
arma::sp_mat left_factor;
Expand Down Expand Up @@ -304,6 +305,7 @@ Rcpp::List mnre_fit_sparse(const arma::sp_mat& fixed_effects,
} // end function


//' @export
// [[Rcpp::export]]
double mnre_lk_penalty(const arma::mat& beta_random,
const arma::mat& theta_norm,
Expand All @@ -328,6 +330,7 @@ double mnre_lk_penalty(const arma::mat& beta_random,
}


//' @export
// [[Rcpp::export]]
double mnre_lk_glm(const arma::sp_mat& fixed_effects,
const arma::sp_mat& random_effects,
Expand Down Expand Up @@ -448,6 +451,7 @@ arma::mat mnre_mu_x(const arma::sp_mat &fe_x,
return mu;
}

//' @export
// [[Rcpp::export]]
Rcpp::List mnre_step_sparse(const arma::sp_mat &fixed_effects,
const arma::sp_mat &random_effects,
Expand Down

0 comments on commit 5f776a6

Please sign in to comment.