没测试出你说的问题,下面是环境配置、代码、运行代码的方法、输出结果和系统环境
注:我把你给的代码写在名为 R-test.R
的文件里
环境配置
conda create -n r-env
conda activate r-env
conda install r-base
conda install r-rms
conda install r-dynnom
代码
library(rms)
library(DynNom)
library(shiny)
library(plotly)
library(compare)
library(stargazer)
training_dataset_path = '/home/liangqijie/cox_all_dataset_origin.txt'
training_dataset<- read.csv(training_dataset_path, header = TRUE,sep="\t", stringsAsFactors=FALSE)
print(paste0("该训练集有 ",dim(training_dataset)[1]," 个样本; ",dim(training_dataset)[2]," 个变量"))
ddist <- datadist(training_dataset)
options(datadist='ddist')
#查看数据集中的变量名
colnames(training_dataset)
#构建多因素回归模型
f_cph <- cph(Surv(OS,Censor) ~ Age+Gender+Blood_Glucose+Gender+WHO_class,
x=T, y=T, surv=T,
data=training_dataset)
#查看多因素Cox分析结果,最下方可见其对应的Coef、p值
print(f_cph)
DynNom(f_cph,training_dataset,
clevel = 0.95) #生成动态列线图
运行代码的方法
执行 Rscript R-test.R
就可以得到下面的输出结果
输出结果
载入需要的程辑包:Hmisc
载入程辑包:‘Hmisc’
The following objects are masked from ‘package:base’:
format.pval, units
载入需要的程辑包:magrittr
载入程辑包:‘shiny’
The following object is masked from ‘package:rms’:
validate
载入需要的程辑包:ggplot2
载入程辑包:‘plotly’
The following object is masked from ‘package:ggplot2’:
last_plot
The following object is masked from ‘package:Hmisc’:
subplot
The following object is masked from ‘package:stats’:
filter
The following object is masked from ‘package:graphics’:
layout
载入程辑包:‘compare’
The following object is masked from ‘package:base’:
isTRUE
Please cite as:
Hlavac, Marek (2022). stargazer: Well-Formatted Regression and Summary Statistics Tables.
R package version 5.2.3. https://CRAN.R-project.org/package=stargazer
[1] "该训练集有 228 个样本; 13 个变量"
Frequencies of Missing Values Due to Each Variable
Surv(OS, Censor) Age Gender Blood_Glucose
0 0 0 1
WHO_class
0
Cox Proportional Hazards Model
cph(formula = Surv(OS, Censor) ~ Age + Gender + Blood_Glucose +
Gender + WHO_class, data = training_dataset, x = T, y = T,
surv = T)
Model Tests Discrimination
Indexes
Obs 227 LR chi2 20.67 R2 0.087
Events 164 d.f. 5 R2(5,227)0.067
Center -0.531 Pr(> chi2) 0.0009 R2(5,164)0.091
Score chi2 21.39 Dxy 0.284
Pr(> chi2) 0.0007
Coef S.E. Wald Z Pr(>|Z|)
Age 0.0114 0.0094 1.21 0.2264
Gender=M -0.5130 0.1685 -3.05 0.0023
Blood_Glucose -0.0149 0.0060 -2.48 0.0132
WHO_class=WHO_class_II -0.0363 0.3957 -0.09 0.9269
WHO_class=WHO_class_III 0.2398 0.3550 0.68 0.4993
Listening on http://127.0.0.1:7907
浏览器访问 http://127.0.0.1:7907
正常,并能够绘制出图(其他的还没研究)
系统环境
> sessionInfo()
R version 4.3.1 (2023-06-16)
Platform: x86_64-conda-linux-gnu (64-bit)
Running under: Debian GNU/Linux 12 (bookworm)
Matrix products: default
BLAS/LAPACK: /home/liangqijie/anaconda3/envs/r-env/lib/libopenblasp-r0.3.21.so; LAPACK version 3.9.0
locale:
[1] LC_CTYPE=zh_CN.UTF-8 LC_NUMERIC=C
[3] LC_TIME=zh_CN.UTF-8 LC_COLLATE=zh_CN.UTF-8
[5] LC_MONETARY=zh_CN.UTF-8 LC_MESSAGES=zh_CN.UTF-8
[7] LC_PAPER=zh_CN.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=zh_CN.UTF-8 LC_IDENTIFICATION=C
time zone: Asia/Shanghai
tzcode source: system (glibc)
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] stargazer_5.2.3 compare_0.2-6 plotly_4.10.2 ggplot2_3.4.4
[5] shiny_1.7.5.1 DynNom_5.0.2 magrittr_2.0.3 rms_6.7-1
[9] Hmisc_5.1-1
loaded via a namespace (and not attached):
[1] tidyselect_1.2.0 viridisLite_0.4.2 dplyr_1.1.3 fastmap_1.1.1
[5] lazyeval_0.2.2 TH.data_1.1-2 prediction_0.3.14 promises_1.2.1
[9] digest_0.6.33 rpart_4.1.21 mime_0.12 lifecycle_1.0.3
[13] cluster_2.1.4 ellipsis_0.3.2 survival_3.5-7 compiler_4.3.1
[17] rlang_1.1.1 sass_0.4.7 tools_4.3.1 yaml_2.3.7
[21] utf8_1.2.4 data.table_1.14.8 knitr_1.44 htmlwidgets_1.6.2
[25] multcomp_1.4-25 polspline_1.1.23 foreign_0.8-85 withr_2.5.1
[29] purrr_1.0.2 nnet_7.3-19 grid_4.3.1 fansi_1.0.5
[33] xtable_1.8-4 colorspace_2.1-0 scales_1.2.1 MASS_7.3-60
[37] BBmisc_1.13 cli_3.6.1 mvtnorm_1.2-3 rmarkdown_2.25
[41] generics_0.1.3 rstudioapi_0.15.0 httr_1.4.7 cachem_1.0.8
[45] stringr_1.5.0 splines_4.3.1 base64enc_0.1-3 vctrs_0.6.4
[49] Matrix_1.6-1.1 sandwich_3.0-2 jsonlite_1.8.7 SparseM_1.81
[53] Formula_1.2-5 htmlTable_2.4.1 tidyr_1.3.0 jquerylib_0.1.4
[57] glue_1.6.2 codetools_0.2-19 stringi_1.7.12 gtable_0.3.4
[61] later_1.3.1 munsell_0.5.0 tibble_3.2.1 pillar_1.9.0
[65] htmltools_0.5.6.1 quantreg_5.97 R6_2.5.1 evaluate_0.22
[69] lattice_0.22-5 backports_1.4.1 httpuv_1.6.11 bslib_0.5.1
[73] MatrixModels_0.5-2 Rcpp_1.0.11 gridExtra_2.3 nlme_3.1-163
[77] checkmate_2.3.0 xfun_0.40 zoo_1.8-12 pkgconfig_2.0.3
能在 other attached packages 这一列看到对应的包