R语言基于DynNom包绘制动态列线图

Python018

R语言基于DynNom包绘制动态列线图,第1张

以下为该包的帮助文件内容

Dynamic nomogram to visualise statistical models

Description

DynNom is a generic function to display the results of statistical model objects as a dynamic nomogram in an 'RStudio' panel or web browser. DynNom supports a large number of model objects from a variety of packages.

Usage

DynNom(model, data = NULL, clevel = 0.95, m.summary = c("raw", "formatted"),

covariate = c("slider", "numeric"), ptype = c("st", "1-st"),

DNtitle = NULL, DNxlab = NULL, DNylab = NULL, DNlimits = NULL,

KMtitle = NULL, KMxlab = NULL, KMylab = NULL)

DynNom.core(model, data, clevel, m.summary, covariate, DNtitle, DNxlab, DNylab, DNlimits)

DynNom.surv(model, data, clevel, m.summary, covariate,

ptype, DNtitle, DNxlab, DNylab, KMtitle, KMxlab, KMylab)

Arguments

model

an lm, glm, coxph, ols, Glm, lrm, cph, mgcv::gam or gam::gam model objects.

data

a dataframe of the accompanying dataset for the model (if required).

clevel

a confidence level for constructing the confidence interval. If not specified, a 95% level will be used.

m.summary

an option to choose the type of the model output represented in the 'Summary Model' tab. "raw" (the default) returns an unformatted summary of the model"formatted" returns a formatted table of the model summary using stargazer package.

covariate

an option to choose the type of input control widgets used for numeric values. "slider" (the default) picks out sliderInput"numeric" picks out numericInput.

ptype

an option for coxph or cph model objects to choose the type of plot which displays in "Survival plot" tab. "st" (the default) returns plot of estimated survivor probability (S(t)). "1-st" returns plot of estimated failure probability (1-S(t)).

DNtitle

a character vector used as the app's title. If not specified, "Dynamic Nomogram" will be used.

DNxlab

a character vector used as the title for the x-axis in "Graphical Summary" tab. If not specified, "Probability" will be used for logistic model and Cox proportional model objectsor "Response variable" for other model objects.

DNylab

a character vector used as the title for the y-axis in "Graphical Summary" tab (default is NULL).

DNlimits

a vector of 2 numeric values used to set x-axis limits in "Graphical Summary" tab. Note: This also removes the 'Set x-axis ranges' widget in the sidebar panel.

KMtitle

a character vector used as KM plot's title in "Survival plot" tab. If not specified, "Estimated Survival Probability" for ptype = "st" and "Estimated Probability" for ptype = "1-st" will be used.

KMxlab

a character vector used as the title for the x-axis in "Survival plot" tab. If not specified, "Follow Up Time" will be used.

KMylab

a character vector used as the title for the y-axis in "Survival plot" tab. If not specified, "S(t)" for ptype = "st" and "F(t)" for ptype = "1-st" will be used.

Value

A dynamic nomogram in a shiny application providing individual predictions which can be used as a model visualisation or decision-making tools.

The individual predictions with a relative confidence interval are calculated using the predict function, displaying either graphically as an interactive plot in the Graphical Summary tab or a table in the Numerical Summary tab. A table of model output is also available in the Model Summary tab. In the case of the Cox proportional hazards model, an estimated survivor/failure function will be additionally displayed in a new tab.

Please cite as:

Jalali, A., Roshan, D., Alvarez-Iglesias, A., Newell, J. (2019). Visualising statistical models using dynamic nomograms. R package version 5.0.

Author(s)

Amirhossein Jalali, Davood Roshan, Alberto Alvarez-Iglesias, John Newell

Maintainer: Amirhossein Jalali [email protected]

References

Banks, J. 2006. Nomograms. Encyclopedia of Statistical Sciences. 8.

Easy web applications in R. http://shiny.rstudio.com

Frank E Harrell Jr (2017). rms: Regression Modeling Strategies. R package version 4.5-0. https://CRAN.R-project.org/package=rms

See Also

DNbuilder, getpred.DN

这里分享一下R语言实现VAR和SVAR的整个流程。

主要步骤包括:

1.单位根检验

2.确定滞后阶数

3.格兰杰因果检验

4.模型稳定性检验

5.脉冲响应

6.方差分解

(Johansen协整检验,如果需要的话)

整个过程用到的R语言的扩展包有:

library(zoo)

library(vars)

library(tseries)

首先,数据是下面的样子:

ps:数据是时间序列类型,可以通过下面方法将dataframe转成时间序列类型

data = ts(data)

1.单位根检验

#对data的第一列进行单位根检验

adf.test(data[,1])

2.滞后阶数确定

VARselect函数结果包括AIC、HQ、SC和FPE准则

#参数y为时间序列数据,lag.max为最大滞后阶数

#参数type值包括const截距,trend趋势,both同时包含截距和趋势,none不包含截距和趋势

VARselect(y=data, lag.max = 10, type = c("const"))

3.格兰杰因果检验

格兰杰因果检验有两个方法,第一个是在构造模型之前,第二个是在构造模型之后在模型的基础上进行格兰杰因果检验。

(1)构造模型之前格兰杰因果检验

#函数格式:grangertest(yt~xt)

eg:

grangertest(Value~BCI)

(2)构造模型之后格兰杰因果检验

#函数格式:causality(VARModel,cause)

eg

var =  VAR(data ,p = 2, type = "const")

causality(var,cause=c('Count','Value'))

ps:在这里如果想要构建SVAR模型的话,需要根据实际情况构建两个矩阵amat和bmat,然后使用这两个矩阵来构建SVAR模型:

svar = SVAR(var,Amat = amat,Bmat = bmat)

4.模型稳定性检验

#这里使用“OLS-CUSUM”,它给出的是残差累积和,在该检验生成的曲线图中,残差累积和曲线以时间为横坐标,

#图中绘出两条临界线,如果累积和超出了这两条临界线,则说明参数不具有稳定性。

sta = stability(var, type = c("OLS-CUSUM"), h = 0.15, dynamic = FALSE, rescale = TRUE)

plot(sta)##结果稳健

5.脉冲响应

#标题栏说明,这是BCI(或者其他变量)对各个变量(包括BCI自身)的脉冲响应

(1)VAR脉冲响应

var.irf<-irf(var,n.head=10)

plot(var.irf)

(2)SVAR脉冲响应

svar.irf<-irf(svar,n.ahead = 100)

plot(svar.irf)

6.方差分解

#反映了各变量的贡献率

(1)VAR方差分解

fevd1<-fevd(var, n.ahead = 10)

fevd1$Count

(2)SVAR方差分解

fevd2<-fevd(svar, n.ahead = 10)

fevd2$Value

ps:有时候需要进行Johansen协整检验

#Johansen协整检验,

#对r=0(不存在协整关系)的检验统计量大于临界值,表明拒绝原假设

yJoTest = ca.jo(data, type = c("trace"), ecdet = c("none"), K = 2)

summary(yJoTest)

网页链接