Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to OG #1

Open
daynefiler opened this issue May 25, 2019 · 0 comments
Open

Update to OG #1

daynefiler opened this issue May 25, 2019 · 0 comments

Comments

@daynefiler
Copy link

Sorry to be lazy and not submit a PR -- I added some data checks and modified the code to take a model matrix Z rather than vector z. I realize you can submit a matrix (minus the intercept) to z, but I think requiring a model matrix explicitly will be more familiar to most users.

OG <- function(X, Z, k = max(2, round(NCOL(X)/10)), rescale = FALSE) {
  
  ## Data checks
  if (!is.matrix(X) || !is.numeric(X)) stop("X must be a numeric matrix")
  if (!is.matrix(Z) || !is.numeric(Z)) stop("Z must be a numeric matrix")
  if (NROW(X) != NROW(Z)) stop("X and Z must have the same rows")
  if (k > NCOL(X)) stop("'k' must be <= ncol(X).")
  stopifnot(is.numeric(k))
  if (length(k) > 1) {
    k <- k[1]
    warning("length(k) > 1, only using first element.")
  }
  
  ## Perform algorithm
  if (rescale) X <- scale(X)
  SVD <- svd(X, nu = k, nv = k)
  LM <- lm(SVD$u %*% diag(SVD$d[1:k]) ~ -1 + Z)
  S <- SVD$u %*% diag(SVD$d[1:k]) - Z %*% LM$coef
  list(S = S, U = t(SVD$v))
  
}

Also, will this be submitted to CRAN? Happy to help work on getting it ready for submission.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant