diff --git a/NAMESPACE b/NAMESPACE index 4bc04ca..6773b62 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,6 +2,7 @@ export(mnre_dim_and_class_to_index) export(mnre_expand_matrix) +export(mnre_fit) export(mnre_fit_sparse) export(mnre_make_covar) export(mnre_simulate_ev_data) diff --git a/R/main.R b/R/main.R index 3fdc4a6..4c22a8c 100644 --- a/R/main.R +++ b/R/main.R @@ -208,28 +208,41 @@ nd_min_fun <- function(ev) { data=ev$fr, family='binomial') fe <- fixed_effects <- (glf$X) re <- random_effects <- Matrix::t(glf$reTrms$Zt) - y <- matrix(ev$y, ncol=1) + + y <- matrix(ev$fr[,all.vars(ev$frm)[[1]]], ncol=1) k_class <- max(y) k <- max(y) - Lind = ev$Lind + Lind = glf$reTrms$Lind s = 'mval ' for (v in mval) { s = sprintf("%s %.4e ", s, v) } - message(s) + + if (ev$verbose > 0) { + message(s) + } + theta_mat <- matrix(mval, ncol=k_class) fe2 <- Matrix::Matrix(fe, sparse = TRUE) + if (! "beta_re" %in% names(ev)) { + ev$beta_re <- matrix(rnorm(ncol(re) * k_class, 0, 0.2), ncol=k_class) + } + + if (! "beta_fe" %in% names(ev)) { + ev$beta_fe <- matrix(rnorm(ncol(fe) * k_class, 0, 0.2), ncol=k_class) + } + beta_re <- ev$beta_re beta_fe <- ev$beta_fe - # beta_re <- matrix(rep(0, ncol(re) * k_class), ncol=k_class) - # beta_fe <- matrix(rep(0, ncol(fe) * k_class), ncol=k_class) - # - message("starting beta ", beta_fe[[1]], " ", beta_re[[1]]) + if (ev$verbose > 0) { + message("starting beta ", beta_fe[[1]], " ", beta_re[[1]]) + } + zz <- mnre_fit_sparse(fe2, re, y, theta_mat, Lind, beta_fe, beta_re, verbose = ev$verbose) ev$beta_fe <<- zz$beta_fixed diff --git a/R/mnre_fit.R b/R/mnre_fit.R new file mode 100644 index 0000000..875f88d --- /dev/null +++ b/R/mnre_fit.R @@ -0,0 +1,16 @@ + +#' @export +mnre_fit <- function(frm, data, verbose=0, off_diagonal=0.0) { + ev <- list() + ev$frm <- frm + ev$fr <- data + ev$verbose <- verbose + + nlev <- length(all.vars(ev$frm)) + mval <- rep(1, (nlev-1) * max(ev$fr$y)) + + nf <- nd_min_fun(ev) + + ans = optim(mval, nf, method = "L-BFGS", lower=1e-8) + +} \ No newline at end of file