osorensen / hdme

R-package containing penalized regression methods for High-Dimensional Measurement Error problems (errors-in-variables)
GNU General Public License v3.0
8 stars 3 forks source link

Add S3 methods #29

Closed osorensen closed 5 years ago

osorensen commented 5 years ago

Add print, summary, and coef methods to the S3 classes defined.

osorensen commented 5 years ago

Here are illustrations of print and coef methods implemented in development version 0.2.3.9001. This is shown for the case of gmus, but similar S3 methods are implemented for the other algorithms (gmu_lasso, corrected_lasso, cv_corrected_lasso).

library(hdme)
# Example with linear regression
set.seed(1)
n <- 100 # Number of samples
p <- 10 # Number of covariates
# True (latent) variables
X <- matrix(rnorm(n * p), nrow = n)
# Measurement matrix (this is the one we observe)
W <- X + matrix(rnorm(n*p, sd = 1), nrow = n, ncol = p)
# Coefficient vector
beta <- c(seq(from = 0.1, to = 1, length.out = 5), rep(0, p-5))
# Response
y <- X %*% beta + rnorm(n, sd = 1)

# Run the MU Selector on a delta grid
fit1 <- gmus(W, y, delta = seq(from = 0, to = 0.5, by = 0.1))
# print method
fit1
#> Generalized MU Selector with family gaussian, with 10 variables fitted with regularization parameters lambda = 0.09732412 and 6 delta values.
#> Use functions plot() and coef() for more information about the fitted values.

# coef method
coef(fit1)
#> Number of nonzero coefficient estimates
#> as a function of regularization parameters
#> (lambda, delta):
#>  lambda delta nonzeros
#>   0.097   0.0        7
#>   0.097   0.1        4
#>   0.097   0.2        5
#>   0.097   0.3        3
#>   0.097   0.4        3
#>   0.097   0.5        3

# Run the MU Selector on a single delta
fit2 <- gmus(W, y, delta = 0.2)

# coef method by default  shows only nonzero coefficient estimates
coef(fit2)
#> Non-zero coefficient estimates at
#> regularization parameters (lambda, delta) = (0.081, 0.2):
#>  coefficient      estimate
#>            1 -5.690423e-17
#>            2  3.440890e-02
#>            3  1.158849e-01
#>            4  2.003944e-01
#>            5  3.659696e-01

# can optionally show all coefficient estimates
coef(fit2, all = TRUE)
#> Coefficient estimates at regularization
#> parameters (lambda, delta) = (0.081, 0.2):
#>  coefficient      estimate
#>            1 -5.690423e-17
#>            2  3.440890e-02
#>            3  1.158849e-01
#>            4  2.003944e-01
#>            5  3.659696e-01
#>            6  0.000000e+00
#>            7  0.000000e+00
#>            8  0.000000e+00
#>            9  0.000000e+00
#>           10  0.000000e+00

Created on 2019-05-14 by the reprex package (v0.2.1)