forked from stephenslab/susieR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict.susie.R
68 lines (67 loc) · 1.96 KB
/
predict.susie.R
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
60
61
62
63
64
65
66
67
68
#' @title Extract regression coefficients from susie fit
#'
#' @param object A susie fit.
#'
#' @param \dots Additional arguments passed to the generic \code{coef}
#' method.
#'
#' @return A p+1 vector, the first element being an intercept, and the
#' remaining p elements being estimated regression coefficients.
#'
#' @importFrom stats coef
#'
#' @method coef susie
#'
#' @export coef.susie
#'
#' @export
#'
coef.susie = function (object, ...) {
s = object
return(c(s$intercept,colSums(s$alpha*s$mu)/s$X_column_scale_factors))
}
#' @title Predict outcomes or extract coefficients from susie fit.
#'
#' @param object A susie fit.
#'
#' @param newx A new value for X at which to do predictions.
#'
#' @param type The type of output. For \code{type = "response"},
#' predicted or fitted outcomes are returned; for \code{type =
#' "coefficients"}, the estimated coefficients are returned.
#'
#' @param \dots Other arguments used by generic predict function. These
#' extra arguments are not used here.
#'
#' @return For \code{type = "response"}, predicted or fitted outcomes
#' are returned; for \code{type = "coefficients"}, the estimated
#' coefficients are returned. If the susie fit has intercept =
#' \code{NA} (which is common when using \code{susie_suff_stat}) then
#' predictions are computed using an intercept of 0, and a warning is
#' emitted.
#'
#' @importFrom stats coef
#'
#' @method predict susie
#'
#' @export predict.susie
#'
#' @export
#'
predict.susie = function (object, newx = NULL,
type = c("response","coefficients"), ...) {
s = object
type = match.arg(type)
if (type == "coefficients") {
if (!missing(newx))
stop("Do not supply newx when predicting coefficients")
return(coef(s))
}
if (missing(newx))
return(s$fitted)
if (is.na(s$intercept)) {
warning("The prediction assumes intercept = 0")
return(drop(newx %*% coef(s)[-1]))
} else
return(drop(s$intercept + newx %*% coef(s)[-1]))
}