8
8
# ' @param x Independent variables, can be a \code{matrix} or a \code{data.frame}
9
9
# ' @param y Dependent variable, can be a \code{vector} or a column from a \code{data.frame}
10
10
# ' @param stage Algorithm indicator. 1 denotes the one-stage algorithm and
11
- # ' 2 denotes the two-stage algorithm. Default is 1.
11
+ # ' 2 denotes the two-stage algorithm. Default is 2. When \code{n} is less than \code{p},
12
+ # ' only the two-stage algorithm is available.
13
+ # ' @param family A description of the error distribution and link function to be
14
+ # ' used in the model. It can take the value of `\code{gaussian}`, `\code{binomial}`,
15
+ # ' `\code{poisson}`, and `\code{cox}`. Default is `\code{gaussian}`
12
16
# '
13
17
# ' @return A list of following components:
14
18
# ' \describe{
17
21
# ' \item{lambda}{Cross-validated lambda in the two-stage algorithm. \code{NULL} for the one-stage algorithm}
18
22
# ' \item{x}{Input data \code{x}}
19
23
# ' \item{y}{Input data \code{y}}
24
+ # ' \item{family}{\code{family} from the input}
25
+ # ' \item{stage}{\code{stage} from the input}
20
26
# ' }
21
27
# ' @export
22
28
# ' @seealso
27
33
# ' * [plot.sgpv()] plots variable selection results
28
34
# ' @examples
29
35
# '
30
- # ' # load the package
31
- # ' library(ProSGPV)
32
- # '
33
36
# ' # prepare the data
34
37
# ' x <- t.housing[, -ncol(t.housing)]
35
38
# ' y <- t.housing$V9
36
39
# '
37
- # ' # run one-stage algorithm
38
- # ' out.sgpv.1 <- pro.sgpv(x = x, y = y, stage = 1 )
40
+ # ' # run ProSGPV in linear regression
41
+ # ' out.sgpv <- pro.sgpv(x = x, y = y)
39
42
# '
40
- # ' # More examples at https://github.com/zuoyi93/ProSGPV
41
- pro.sgpv <- function (x , y , stage = c(1 , 2 )) {
42
- if ( ! ( stage %in% 1 : 2 )) stop( " Stage only takes value of 1 or 2. " )
43
+ # ' # More examples at https://github.com/zuoyi93/ProSGPV/tree/master/vignettes
44
+ pro.sgpv <- function (x , y , stage = c(1 , 2 ),
45
+ family = c( " gaussian " , " binomial " , " poisson " , " cox " )) {
43
46
44
47
if (nrow(x ) != length(y )) stop(" Input x and y have different number of observations" )
45
48
@@ -48,19 +51,46 @@ pro.sgpv <- function(x, y, stage = c(1, 2)) {
48
51
if (any(complete.cases(x ) == F ) | any(complete.cases(y ) == F )) {
49
52
warning(" Only complete records will be used." )
50
53
comp.index <- complete.cases(data.frame (x , y ))
51
- x <- x [comp.index , ]
52
- y <- y [comp.index ]
54
+ if (family != " cox" ){
55
+ x <- x [comp.index , ]
56
+ y <- y [comp.index ]
57
+ }else {
58
+ x <- x [comp.index , ]
59
+ y <- y [comp.index ,]
60
+ }
61
+
53
62
}
54
63
64
+ if (missing(stage )) stage <- 2
65
+ if (missing(family )) family <- " gaussian"
66
+
67
+ stage <- match.arg(stage )
68
+ family <- match.arg(family )
69
+
70
+ if (stage == 1 & nrow(x )< ncol(x )) stage <- 2
71
+
55
72
if (is.null(colnames(x ))) colnames(x ) <- paste(" V" , 1 : ncol(x ), sep = " " )
56
73
57
- xs <- scale(x )
58
- ys <- scale(y )
74
+ if (family == " gaussian" ){
75
+ xs <- scale(x )
76
+ ys <- scale(y )
77
+ }else {
78
+ xs <- as.matrix(x )
79
+ ys <- y
80
+ }
81
+
59
82
60
83
if (stage == 2 ) {
61
- lasso.cv <- cv.glmnet(xs , ys )
62
- lambda <- lasso.cv $ lambda.1se
63
- candidate.index <- which(coef(lasso.cv , s = lambda )[- 1 ] != 0 )
84
+
85
+ if (family != " cox" ){
86
+
87
+ lasso.cv <- cv.glmnet(xs , ys , family = family )
88
+ lambda <- lasso.cv $ lambda.1se
89
+ candidate.index <- which(coef(lasso.cv , s = lambda )[- 1 ] != 0 )
90
+ }else {
91
+ candidate.index <- which(coef(lasso.cv , s = lambda ) != 0 )
92
+ }
93
+
64
94
} else {
65
95
candidate.index <- 1 : ncol(xs )
66
96
lambda <- NULL
@@ -72,8 +102,10 @@ pro.sgpv <- function(x, y, stage = c(1, 2)) {
72
102
var.index = out.sgpv ,
73
103
var.label = colnames(x )[out.sgpv ],
74
104
lambda = lambda ,
75
- x = x ,
76
- y = y
105
+ x = data.frame (x ),
106
+ y = y ,
107
+ family = family ,
108
+ stage = stage
77
109
)
78
110
79
111
class(out ) <- " sgpv"
@@ -104,9 +136,9 @@ pro.sgpv <- function(x, y, stage = c(1, 2)) {
104
136
# ' out.sgpv.1
105
137
print.sgpv <- function (x , ... ) {
106
138
if (length(x $ var.index ) > 0 ) {
107
- cat(" Selected variables are" , x $ var.label )
139
+ cat(" Selected variables are" , x $ var.label , " \n " )
108
140
} else {
109
- cat(" None of variables are selected." )
141
+ cat(" None of variables are selected.\n " )
110
142
}
111
143
}
112
144
0 commit comments