R语言-Stepwise Regression Analysis

2021-08-19  本文已影响0人  超级可爱的懂事长鸭

Stepwise Cox Regression

https://search.r-project.org/CRAN/refmans/My.stepwise/html/My.stepwise.coxph.html

#install
install.packages("My.stepwise")
library(My.stepwise)
#Usage
My.stepwise.coxph(Time = NULL, T1 = NULL, T2 = NULL, Status = NULL,
  variable.list, in.variable = "NULL", data, sle = 0.15, sls = 0.15,
  vif.threshold = 999

##Examples
## Not run: 
The data 'lung' is available in the 'survival' package.
## End(Not run)

if (requireNamespace("survival", quietly = TRUE)) {
 lung <- survival::lung
}

names(lung)
dim(lung)
my.data <- na.omit(lung)
dim(my.data)
head(my.data)
my.data$status1 <- ifelse(my.data$status==2,1,0)
my.variable.list <- c("inst", "age", "sex", "ph.ecog", "ph.karno", "pat.karno")
My.stepwise.coxph(Time = "time", Status = "status1", variable.list = my.variable.list,
   in.variable = c("meal.cal", "wt.loss"), data = my.data)

my.variable.list <- c("inst", "age", "sex", "ph.ecog", "ph.karno", "pat.karno", "meal.cal",
   "wt.loss")
My.stepwise.coxph(Time = "time", Status = "status1", variable.list = my.variable.list,
   data = my.data, sle = 0.25, sls = 0.25)

Arguments

Time
The 'Time' (time to an event) for the sepcified Cox's proportional hazards model as in coxph().

T1
The 'T1' (Start) of the long-form data for the sepcified Cox's model as in coxph().

T2
The 'T2' (Stop) of the long-form data for the sepcified Cox's model as in coxph().

Status
The 'Status' (event indicator) for the sepcified Cox's proportional hazards model as in coxph().

variable.list
A list of covariates to be selected.

in.variable
A list of covariate(s) to be always included in the regression model.

data
The data to be analyzed.

sle 纳入变量的P值
The chosen significance level for entry (SLE).

sls 剔除变量的P值
The chosen significance level for stay (SLS).

vif.threshold
The chosen threshold value of variance inflating factor (VIF).

报错

Error in str2lang(x) : <text>:1:22: unexpected numeric constant
1: Death~TNM_groupStage 2
                         ^

输入的向量得是数值型

Stepwise Logistic Regression

通过AIC筛选模型,AIC越小越好

https://www.rdocumentation.org/packages/stats/versions/3.6.2/topics/step

step(object, scope, scale = 0,
     direction = c("both", "backward", "forward"),
     trace = 1, keep = NULL, steps = 1000, k = 2, …)

Arguments

object

an object representing a model of an appropriate class (mainly "lm" and "glm"). This is used as the initial model in the stepwise search.

scope

defines the range of models examined in the stepwise search. This should be either a single formula, or a list containing components upper and lower, both formulae. See the details for how to specify the formulae and how they are used.

scale

used in the definition of the AIC statistic for selecting the models, currently only for [lm](https://www.rdocumentation.org/link/lm?package=stats&version=3.6.2), [aov](https://www.rdocumentation.org/link/aov?package=stats&version=3.6.2) and [glm](https://www.rdocumentation.org/link/glm?package=stats&version=3.6.2) models. The default value, 0, indicates the scale should be estimated: see [extractAIC](https://www.rdocumentation.org/link/extractAIC?package=stats&version=3.6.2).

direction

the mode of stepwise search, can be one of "both", "backward", or "forward", with a default of "both". If the scope argument is missing the default for direction is "backward". Values can be abbreviated.

trace

if positive, information is printed during the running of step. Larger values may give more detailed information.

keep

a filter function whose input is a fitted model object and the associated AIC statistic, and whose output is arbitrary. Typically keep will select a subset of the components of the object and return them. The default is not to keep anything.

steps

the maximum number of steps to be considered. The default is 1000 (essentially as many as required). It is typically used to stop the process early.

k

the multiple of the number of degrees of freedom used for the penalty. Only k = 2 gives the genuine AIC: k = log(n) is sometimes referred to as BIC or SBC.

any additional arguments to [extractAIC](https://www.rdocumentation.org/link/extractAIC?package=stats&version=3.6.2).

报错

Error in step(fit) :    number of rows in use has changed: remove missing values?

需要去除NA值,na.omit

上一篇 下一篇

猜你喜欢

热点阅读