-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharticles-step.R
210 lines (191 loc) · 9.22 KB
/
articles-step.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# Minor modifications to My.stepwise v0.1.0 by Fu-Chang Hu
stepwise.coxph <- function(Time=NULL,
T1=NULL,
T2=NULL,
Status=NULL,
variable.list,
in.variable="NULL",
data,
sle=0.15,
sls=0.15,
vif.threshold=999)
{
univar.pvalue <- NULL
temp.model <- NULL
if (is.null(T2)) {
initial.model <- coxph(as.formula(paste("Surv(", Time,", ", Status,") ~ ", paste(in.variable, collapse="+"), sep="")), data=data, method="efron")
} else if (is.null(T2)) {
initial.model <- coxph(as.formula(paste("Surv(", Time,", ", Status,") ~ ", paste(in.variable, collapse="+"), sep="")), data=data, method="efron")
} else if (is.null(Time)){
initial.model <- coxph(as.formula(paste("Surv(", T1,", ", T2,", ", Status,") ~ ", paste(in.variable, collapse="+"), sep="")), data=data, method="efron")
} else if (is.null(Time)){
initial.model <- coxph(as.formula(paste("Surv(", T1,", ", T2,", ", Status,") ~ ", paste(in.variable, collapse="+"), sep="")), data=data, method="efron")
}
if (is.null(initial.model$coefficients)) {
for (i in 1:length(variable.list))
{
if (is.null(T2))
{
uni.model <- coxph(as.formula(paste("Surv(", Time,", ", Status,") ~ ", variable.list[i], sep="")), data=data, method="efron")
}
if (is.null(Time))
{
uni.model <- coxph(as.formula(paste("Surv(", T1,", ", T2,", ", Status,") ~ ", variable.list[i], sep="")), data=data, method="efron")
}
univar.pvalue[i] <- summary(uni.model)$coefficients[5]
}
variable.list1 <- variable.list[univar.pvalue<=0.9 & !is.na(univar.pvalue)]
univar.pvalue1 <- univar.pvalue[univar.pvalue<=0.9 & !is.na(univar.pvalue)]
uni.x <- variable.list1[which.min(univar.pvalue1)]
if (length(uni.x) > 0) {
if (is.null(T2))
{
formula <- as.formula(paste("Surv(", Time,", ", Status,") ~ ", uni.x, sep=""))
temp.model <- coxph(formula, data=data, method="efron")
if (length(temp.model$coefficients) > 1)
{
print(car::vif(glm(paste(Status, paste(names(temp.model$coefficients), collapse="+"), sep="~"), data=data, family=binomial(link="logit"))))
}
}
if (is.null(Time))
{
formula <- as.formula(paste("Surv(", T1,", ", T2,", ", Status,") ~ ", uni.x, sep=""))
temp.model <- coxph(formula, data=data, method="efron")
}
cat("# --------------------------------------------------------------------------------------------------\n")
cat("# Initial Model:\n")
print(summary(temp.model))
}
} else if (!is.null(initial.model$coefficients)) {
temp.model <- initial.model
cat("# --------------------------------------------------------------------------------------------------\n")
cat("# Initial Model:\n")
print(summary(temp.model))
}
if (length(temp.model$coefficients) > 1)
{
cat("--------------- Variance Inflating Factor (VIF) ---------------\n")
cat("Multicollinearity Problem: Variance Inflating Factor (VIF) is bigger than 10 (Continuous Variable) or is bigger than 2.5 (Categorical Variable)\n")
print(car::vif(glm(paste(Status, paste(names(temp.model$coefficients), collapse="+"), sep="~"), data=data, family=binomial(link="logit"))))
}
i <- 0
break.rule <- TRUE
while (break.rule)
{
i <- i + 1
if (i == 1)
{
variable.list2 <- setdiff(variable.list, all.vars(temp.model$formula))
} else
{
variable.list2 <- setdiff(variable.list, c(all.vars(temp.model$formula), out.x))
out.x <- NULL
}
if (length(variable.list2) != 0)
{
anova.pvalue <- NULL
mv.pvalue <- NULL
vif.value <- NULL
for (k in 1:length(variable.list2))
{
model <- update(temp.model, as.formula(paste(". ~ . + ", variable.list2[k], sep="")))
if (length(model$coefficients) > 1)
{
if (sum(is.na(model$coefficients)) != 0)
{
anova.pvalue[k] <- 1
mv.pvalue[k] <- 1
vif.value[k] <- 999
} else {
anova.pvalue[k] <- anova(temp.model, model)[2,"P(>|Chi|)"]
mv.pvalue[k] <- summary(model)$coefficients[nrow(summary(model)$coefficients),"Pr(>|z|)"]
model.vif <- car::vif(glm(as.formula(paste(Status, paste(names(model$coefficients), collapse="+"), sep="~")), data=data, family=binomial(link="logit")))
vif.value[k] <- model.vif[length(model.vif)]
}
}
}
variable.list2.1 <- variable.list2[mv.pvalue<=0.9 & !is.na(mv.pvalue) & vif.value <= vif.threshold]
anova.pvalue2 <- anova.pvalue[mv.pvalue<=0.9 & !is.na(mv.pvalue) & vif.value <= vif.threshold]
mv.pvalue2 <- mv.pvalue[mv.pvalue<=0.9 & !is.na(mv.pvalue) & vif.value <= vif.threshold]
enter.x <- variable.list2.1[anova.pvalue2==min(anova.pvalue2, na.rm=TRUE) & anova.pvalue2 <= sle]
wald.p <- mv.pvalue2[anova.pvalue2==min(anova.pvalue2, na.rm=TRUE) & anova.pvalue2 <= sle]
if (length(setdiff(enter.x, NA)) != 0)
{
if (length(enter.x) > 1)
{
enter.x <- enter.x[which.min(wald.p)]
}
cat("# --------------------------------------------------------------------------------------------------", "\n")
cat(paste("### iter num = ", i, ", Forward Selection by LR Test: ","+ ", enter.x, sep=""), "\n")
temp.model <- update(temp.model, as.formula(paste(". ~ . + ", enter.x, sep="")))
print(summary(temp.model))
cat("--------------- Variance Inflating Factor (VIF) ---------------\n")
cat("Multicollinearity Problem: Variance Inflating Factor (VIF) is bigger than 10 (Continuous Variable) or is bigger than 2.5 (Categorical Variable)\n")
if (length(temp.model$coefficients) > 1)
{
print(car::vif(glm(paste(Status, paste(names(temp.model$coefficients), collapse="+"), sep="~"), data=data, family=binomial(link="logit"))))
}
}
} else {enter.x <- NULL}
if (i == 1 & length(enter.x) == 0) {
cat("# ==================================================================================================", "\n")
cat(paste("*** Stepwise Final Model (in.lr.test: sle = ", sle, "; variable selection restrict in vif = ", vif.threshold, "):", sep=""), "\n")
print(summary(temp.model))
break
} else {
variable.list3 <- setdiff(rownames(summary(temp.model)$coefficients), c(enter.x, in.variable))
if (length(variable.list3) != 0)
{
anova.pvalue <- NULL
for (k in 1:length(variable.list3))
{
model <- update(temp.model, as.formula(paste(". ~ . - ", variable.list3[k], sep="")))
anova.pvalue[k] <- anova(model, temp.model)[2,"P(>|Chi|)"]
}
out.x <- variable.list3[anova.pvalue==max(anova.pvalue, na.rm=TRUE) & anova.pvalue > sls]
out.x <- setdiff(out.x, NA)
if (length(out.x) != 0)
{
if (length(out.x) > 1)
{
out.x.1 <- out.x
for (j in 1:length(out.x)) {
out.x[j] <- out.x.1[(length(out.x)-j+1)]
}
wald.p <- rep(NA, length(out.x))
for (j in 1:length(out.x)) {
wald.p[j] <- summary(temp.model)$coefficients[,"Pr(>|z|)"][rownames(summary(temp.model)$coefficients)==out.x[j]]
}
out.x <- out.x[which.max(wald.p)]
}
cat("# --------------------------------------------------------------------------------------------------", "\n")
cat(paste("### iter num = ", i, ", Backward Selection by LR Test: ","- ", out.x, sep=""), "\n")
temp.model <- update(temp.model, as.formula(paste(". ~ . - ", out.x, sep="")))
print(summary(temp.model))
cat("--------------- Variance Inflating Factor (VIF) ---------------\n")
cat("Multicollinearity Problem: Variance Inflating Factor (VIF) is bigger than 10 (Continuous Variable) or is bigger than 2.5 (Categorical Variable)\n")
if (length(temp.model$coefficients) > 1)
{
print(car::vif(glm(paste(Status, paste(names(temp.model$coefficients), collapse="+"), sep="~"), data=data, family=binomial(link="logit"))))
}
}
} else {out.x <- NULL}
}
if ((length(enter.x) + length(out.x)) == 0)
{
final.model <- temp.model
cat("# ==================================================================================================", "\n")
cat(paste("*** Stepwise Final Model (in.lr.test: sle = ", sle, "; out.lr.test: sls = ", sls, "; variable selection restrict in vif = ", vif.threshold, "):", sep=""), "\n")
print(summary(final.model))
cat("--------------- Variance Inflating Factor (VIF) ---------------\n")
cat("Multicollinearity Problem: Variance Inflating Factor (VIF) is bigger than 10 (Continuous Variable) or is bigger than 2.5 (Categorical Variable)\n")
if (length(final.model$coefficients) > 1)
{
print(car::vif(glm(paste(Status, paste(names(final.model$coefficients), collapse="+"), sep="~"), data=data, family=binomial(link="logit"))))
}
break.rule <- FALSE
}
enter.x <- NULL
}
final.model
}