code.R 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # install.packages("msm", repos = "https://mirrors.tuna.tsinghua.edu.cn/CRAN/")
  2. library(msm)
  3. library(survival)
  4. library(dplyr)
  5. # View(cav)
  6. data <- read.csv("paper_data.csv")
  7. #性别
  8. data$rgender_group <- factor(data$rgender, levels = c(1, 2), labels = c("Male", "Female"))
  9. #年齡
  10. data$age_group <- cut(data$age,
  11. breaks = c(45, 55, 65, Inf),
  12. labels = c("45-54", "55-64",">=65"),
  13. right = FALSE)
  14. #婚姻
  15. data$marital_group <- factor(data$marital_status, levels = c(1, 0), labels = c("married or partnered", "other marital status"))
  16. #教育
  17. data$education_group <- factor(data$education, levels = c(0, 1, 2), labels = c("below high school", "high school", "college or above"))
  18. #运动情况
  19. data$activity_group <- factor(data$Physical_activity, levels = c(0, 1, 2), labels = c("inactive", "moderate", "vigorous"))
  20. #心理得分
  21. data$psychiatric_group <- cut(data$Psychiatric_score, breaks = c(0, 10, Inf),labels = c("无抑郁", "有抑郁"), right = FALSE)
  22. #BMI
  23. # 使用 cut() 创建因子类型的变量
  24. data$variable <- cut(data$BMI, breaks = c(0, 18.5, 24, Inf), labels = c("underweight", "normal", "overweight"), right = FALSE)
  25. # 使用 dplyr::recode 直接替换因子水平
  26. data$variable <- recode(data$variable, "underweight" = 1L, "normal" = 0L, "overweight" = 2L)
  27. # 将 data$variable 转换回因子并保留原 levels
  28. data$BMI_group <- factor(data$variable, levels = c(0, 1, 2), labels = c("normal", "underweight", "overweight"))
  29. #ADL
  30. # 使用 cut() 创建因子类型的变量
  31. data$ADL_group <- cut(data$ADL, breaks = c(0, 1, 3, Inf), labels = c("No impairment", "Mild impairment", "Severe impairment"), right = FALSE)
  32. summary(data)
  33. View(data[, c("ADL", "variable")])
  34. View(data$age_group)
  35. # 计算状态转移频数表
  36. freq_table <- statetable.msm(state, ID, data = data)
  37. print(freq_table)
  38. # 初始化转移速率矩阵
  39. qmatrix_init <- matrix(c(-0.5, 0.25, 0.15, 0.1,
  40. 0.1, -0.3, 0.1, 0.1,
  41. 0.3, 0.1, -0.5, 0.1,
  42. 0, 0, 0, 0),
  43. nrow = 4, byrow = TRUE)
  44. # 创建初始模型
  45. crude_init <- crudeinits.msm(state ~ wave, subject = ID, data = data, qmatrix = qmatrix_init)
  46. View(crude_init)
  47. # 进行多状态模型分析
  48. msm_model <- msm(state ~ wave, subject = ID, data = data,
  49. qmatrix = crude_init,
  50. covariates = ~ rgender_group+age_group+marital_group+education_group+activity_group+psychiatric_group+BMI_group+ADL_group+Smoke+Drink+last_year_O3+last_year_pm1+last_year_NO2+last_year_NH4+last_year_nl,
  51. death = 4,
  52. method = "BFGS", control = list(fnscale = 5000, maxit = 10000)
  53. )
  54. # 获取模型结果并转换为字符串
  55. model_summary <- capture.output(summary(msm_model))
  56. # 创建一个空的结果文件
  57. result_file <- "msm_model_results_test.txt"
  58. write("", file = result_file) # 清空文件内容
  59. # 写入文件,附加协变量名称
  60. cat("Results for covariates:", file = result_file, append = TRUE)
  61. cat(model_summary, file = result_file, sep = "\n", append = TRUE)
  62. # 查看模型的详细结果
  63. summary(msm_model)
  64. # 计算状态转移概率矩阵
  65. prob_matrix <- pmatrix.msm(msm_model, t = 5) # t = 1 代表随访之间的间隔时间
  66. print(prob_matrix)
  67. # 输出拟合模型的速率矩阵
  68. q_matrix <- qmatrix.msm(msm_model)
  69. print(q_matrix)
  70. # 提取转移强度
  71. transition_intensity <- msm_model$qmatrix
  72. print(transition_intensity)
  73. # 计算在每个状态中的平均逗留时间
  74. so_journ <- sojourn.msm(msm_model)
  75. print(so_journ)
  76. # 计算均衡状态概率
  77. rm(list = ls())
  78. # 定义协变量列表
  79. covariates_list <- list(~last_year_NO3, ~before_last_NO3, ~last_year_NH4, ~before_last_NH4, ~last_year_OM, ~before_last_OM, ~last_year_BC, ~before_last_BC, ~last_year_nl, ~before_last_nl)
  80. # 创建一个空的结果文件
  81. result_file <- "msm_model_results.txt"
  82. write("", file = result_file) # 清空文件内容
  83. # 循环计算不同协变量的模型
  84. for (cov in covariates_list) {
  85. # 运行msm模型
  86. msm_model <- msm(state ~ wave, subject = ID, data = data,
  87. qmatrix = crude_init,
  88. covariates = cov,
  89. death = 4,
  90. method = "BFGS", control = list(fnscale = 4000, maxit = 10000)
  91. )
  92. # 获取模型结果并转换为字符串
  93. model_summary <- capture.output(summary(msm_model))
  94. # 写入文件,附加协变量名称
  95. cat("Results for covariates:", deparse(cov), "\n", file = result_file, append = TRUE)
  96. cat(model_summary, file = result_file, sep = "\n", append = TRUE)
  97. cat("\n\n", file = result_file, append = TRUE) # 空行分隔每个协变量结果
  98. }