当前位置:   article > 正文

人工晶状体计算——人工智能算法(R语言)_用r语言做人工智能

用r语言做人工智能

人工晶状体计算——人工智能算法(R语言)

1. 准备数据

2. 建立模型

2.1 方法1

2.2 方法2


1. 准备数据

准备数据Data.xlsx,示例如图

 

AgeALACDK1K2WTWLTRefIOLPower
68.0022.682.2142.4442.7511.204.82-1.1326.50
62.0023.793.4243.9345.5111.704.49-0.1119.50
62.0023.823.1244.0844.5312.004.90-0.8621.00
68.0022.562.3042.6543.1311.004.65-0.3725.50
73.0023.502.3843.4343.8411.404.85-0.2621.50
74.0023.092.9143.8644.7411.904.45-0.5323.00
73.0024.613.5741.7042.2311.903.49-0.8021.00
64.0022.472.8946.6348.0111.104.41-0.5221.00
63.0021.352.6743.8944.5312.204.68-0.5829.50

2. 建立模型

2.1 方法1

直接建立Age,AL,ACD,K1,K2,WTW,LT,Ref与IOLPower之间的关系

将使用多种人工智能模型,最后将表现最优的三种叠加在一起。

开始先看一下各自变量之间的相关性:

可以看到自变量之间还是有一定相关性的,比如LT(Lens Thickness)与Age正相关,ACD与AL正相关等。

  1. rm(list=ls())
  2. # load libraries
  3. library(caret)
  4. library(corrplot)
  5. library(tidyverse)
  6. library(xlsx)
  7. # Split out validation dataset
  8. set.seed(123)
  9. df <- read.xlsx("./Data.xlsx",sheetName ="Sheet1",stringsAsFactors = FALSE)
  10. str(df)
  11. names(df)<-c("Age","AL","ACD","K1","K2","WTW","LT","IOLPower","Ref")
  12. cor(df[c("Age","AL","ACD","K1","K2","WTW","LT")])
  13. pairs(df[c("Age","AL","ACD","K1","K2","WTW","LT")])
  14. # more informative scatterplot matrix
  15. library(psych)
  16. pairs.panels(df[c("Age","AL","ACD","K1","K2","WTW","LT")])
  17. train.idx <- sample(1:nrow(df), 0.7*nrow(df))
  18. train.df <- as_tibble(df[train.idx,])
  19. test.df <- as_tibble(df[-train.idx,])
  20. train_x = train.df %>% select(-'IOLPower')
  21. train_y = train.df[,'IOLPower']
  22. test_x = test.df %>% select(-'IOLPower')
  23. test_y = test.df[,'IOLPower']
  24. # Run algorithms using 10-fold cross validation
  25. control <- trainControl(method="repeatedcv", number=10, repeats=3, search="random")
  26. metric <- "RMSE"
  27. # lm
  28. set.seed(123)
  29. fit_lm <- train(IOLPower ~ Age+AL+ACD+K1+K2+WTW+LT+Ref, data=train.df, method="lm",
  30. metric=metric, preProc=c("center", "scale"), tuneLength=15, trControl=control)
  31. fit_lm
  32. # SVM
  33. set.seed(123)
  34. fit_svm <- train(IOLPower ~ Age+AL+ACD+K1+K2+WTW+LT+Ref,data=train.df,
  35. method="svmRadial", metric=metric, preProc=c("center", "scale"), tuneLength=15,
  36. trControl=control)
  37. fit_svm
  38. # ANNs
  39. fit_nnet <- train(IOLPower ~ Age+AL+ACD+K1+K2+WTW+LT+Ref,data=train.df, method="nnet",
  40. metric=metric, preProc=c("center", "scale"), tuneLength=15, trControl=control)
  41. fit_nnet
  42. if(FALSE)
  43. {
  44. pred_Power <-predict(fit_nnet,test_x)
  45. print(pred_Power)
  46. err<- test_y$IOLPower-pred_Power
  47. print(err)
  48. plot(err)
  49. }
  50. # random forest
  51. set.seed(123)
  52. fit_rf <- train(IOLPower ~ Age+AL+ACD+K1+K2+WTW+LT+Ref, data = train.df, method="rf",
  53. metric=metric, preProc=c("center", "scale"), tuneLength=15, trControl=control)
  54. fit_rf
  55. # CART
  56. set.seed(123)
  57. fit_cart <- train(IOLPower ~ Age+AL+ACD+K1+K2+WTW+LT+Ref,data=train.df, method="rpart",
  58. metric=metric,preProc=c("center", "scale"),tuneLength=15, trControl=control)
  59. fit_cart
  60. # kNN
  61. set.seed(123)
  62. fit_knn <- train(IOLPower ~ Age+AL+ACD+K1+K2+WTW+LT+Ref,data=train.df, method="knn",
  63. metric=metric, preProc=c("center", "scale"),tuneLength=15, trControl=control)
  64. fit_knn
  65. #MARS
  66. set.seed(123)
  67. fit_MARS<- train(IOLPower ~ Age+AL+ACD+K1+K2+WTW+LT+Ref,data=train.df,
  68. method="earth", metric=metric, preProc=c("center", "scale"), tuneLength=15, trControl=control)
  69. fit_MARS
  70. # GBM
  71. set.seed(123)
  72. fit_gbm <- train(IOLPower ~ Age+AL+ACD+K1+K2+WTW+LT+Ref, data = train.df, method =
  73. "gbm",metric=metric,preProc=c("center", "scale"), tuneLength=15, trControl=control)
  74. fit_gbm
  75. #cubist
  76. set.seed(123)
  77. trControl=control
  78. fit_cubist<- train(IOLPower ~ Age+AL+ACD+K1+K2+WTW+LT+Ref, data = train.df, method =
  79. "cubist",metric=metric,preProc=c("center", "scale"), tuneLength=15, trControl=control)
  80. fit_cubist
  81. # Compare algorithms
  82. results <- resamples(list(LM = fit_lm, CART = fit_cart, KNN= fit_knn, RF = fit_rf, SVR = fit_svm,
  83. GBM = fit_gbm, MARS= fit_MARS, Cubist = fit_cubist,ANNs=fit_nnet))
  84. summary(results)
  85. dotplot(results)
  86. # Stacking
  87. library(caretEnsemble)
  88. set.seed(123)
  89. allModels <- caretList(IOLPower ~ Age+AL+ACD+K1+K2+WTW+LT+Ref,data=train.df,trControl
  90. = trainControl("repeatedcv", number = 10, repeats = 3, search="random",verboseIter = T),metric =
  91. "RMSE", tuneList = list(cubist = caretModelSpec(method = "cubist", tuneLength = 15, preProc =
  92. c("center","scale")),svmRadial = caretModelSpec(method = "svmRadial", tuneLength = 15, preProc
  93. = c("center","scale")),Earth = caretModelSpec(method = "earth", tuneLength = 15, preProc =
  94. c("center","scale"))))
  95. bwplot(resamples(allModels), metric = "RMSE")
  96. summary(allModels)
  97. # Stack with lm
  98. set.seed(123)
  99. stack <- caretStack(allModels, metric = "RMSE", method = "lm", tuneLength = 10,trControl =
  100. trainControl("repeatedcv", number = 5, repeats = 3, verboseIter = T))
  101. summary(stack)
  102. print(stack)
  103. if(TRUE){
  104. ggplot(varImp(fit_lm))
  105. pred_Power <-predict(stack,test_x)
  106. print(pred_Power)
  107. plot(pred_Power)
  108. pred_err <- test_y$IOLPower - pred_Power
  109. print(pred_err)
  110. summary(pred_err)
  111. plot(pred_err)
  112. }

运行结果误差还是很小的。

2.2 方法2

结合光学公式与人工智能(此法源自中山的一篇论文)

光学人工晶体计算公式如下

Power =1336 / (AL- ELP) - (1336/(1336 /(1000 /(1000 /Ref-12)+K)-ELP))

临床实践中最难的就是预测ELP(Effective Lens Position,有效人工晶状体位置,个人觉得"等效人工晶状体位置"这个翻译可能更好),以下代码中Ratio指

Ratio =(ELP-ACD)/LT

下面代码的各个模型都是为了预测这个Ratio,最后求得ELP,然后代入上述光学计算公式中得到最后的人工晶状体度数。

  1. if (TRUE){
  2. rm(list=ls())
  3. library(xlsx)
  4. data <- read.xlsx("./Data.xlsx",sheetName ="Sheet1",stringsAsFactors = FALSE)
  5. f<-function(ELP,AL,K,Ref,P)
  6. {1336 / (AL- ELP) - (1336/(1336 /(1000 /(1000 /Ref-12)+K)-ELP))-P}
  7. root<-c()
  8. for(i in 1:nrow(data)){
  9. root[i]<-uniroot(f,c(0,20),AL=data$AL[i],K=(data$K1[i]+data$K2[i])/2,R=data$Ref[i],P=data$IOLPower[i])$root
  10. }
  11. root
  12. data$ELP <-root
  13. Ratio <- (data$ELP-data$ACD)/data$LT
  14. Ratio
  15. data$Ratio<-Ratio
  16. PowerCalc<-function(ELP,AL,K,Ref)
  17. {1336 / (AL- ELP) - (1336/(1336 /(1000 /(1000 /Ref-12)+K)-ELP))}
  18. P<-PowerCalc(data$ELP,data$AL,(data$K1+data$K2)/2,data$Ref)
  19. P
  20. options(scipen = 100) #小数点后100位不使用科学计数法
  21. err <- data$IOLPower - P
  22. summary(err)
  23. plot(err)
  24. }
  25. library(caret)
  26. library(corrplot)
  27. library(tidyverse)
  28. library(xlsx)
  29. # Split out validation dataset
  30. set.seed(123)
  31. cor(data[c("Age","AL","ACD","K1","K2","WTW","LT")])
  32. pairs(data[c("Age","AL","ACD","K1","K2","WTW","LT")])
  33. # more informative scatterplot matrix
  34. library(psych)
  35. pairs.panels(data[c("Age","AL","ACD","K1","K2","WTW","LT")])
  36. train.idx <- sample(1:nrow(data), 0.7*nrow(data))
  37. train.data <- as_tibble(data[train.idx,])
  38. test.data <- as_tibble(data[-train.idx,])
  39. train_x = train.data %>% select(-'Ratio')
  40. train_y = train.data[,'Ratio']
  41. test_x = test.data %>% select(-'Ratio')
  42. test_y = test.data[,'Ratio']
  43. # Run algorithms using 10-fold cross validation
  44. control <- trainControl(method="repeatedcv", number=10, repeats=3, search="random")
  45. metric <- "RMSE"
  46. # lm
  47. set.seed(123)
  48. fit_lm <- train(Ratio ~ Age+AL+ACD+K1+K2+WTW+LT+Ref, data=train.data, method="lm",
  49. metric=metric, preProc=c("center", "scale"), tuneLength=15, trControl=control)
  50. fit_lm
  51. # SVM
  52. set.seed(123)
  53. fit_svm <- train(Ratio ~ Age+AL+ACD+K1+K2+WTW+LT+Ref,data=train.data,
  54. method="svmRadial", metric=metric, preProc=c("center", "scale"), tuneLength=15,
  55. trControl=control)
  56. fit_svm
  57. plot(fit_svm)
  58. # ANNs
  59. fit_nnet <- train(Ratio ~ Age+AL+ACD+K1+K2+WTW+LT+Ref,data=train.data, method="nnet",
  60. metric=metric, preProc=c("center", "scale"), tuneLength=15, trControl=control)
  61. fit_nnet
  62. if(TRUE)
  63. {
  64. pred <-predict(fit_nnet,test_x)
  65. print(pred)
  66. err<- test_y$Ratio-pred
  67. print(err)
  68. plot(err)
  69. }
  70. # random forest
  71. set.seed(123)
  72. fit_rf <- train(Ratio ~ Age+AL+ACD+K1+K2+WTW+LT+Ref, data = train.data, method="rf",
  73. metric=metric, preProc=c("center", "scale"), tuneLength=15, trControl=control)
  74. fit_rf
  75. # CART
  76. set.seed(123)
  77. fit_cart <- train(Ratio ~ Age+AL+ACD+K1+K2+WTW+LT+Ref,data=train.data, method="rpart",
  78. metric=metric,preProc=c("center", "scale"),tuneLength=15, trControl=control)
  79. fit_cart
  80. # kNN
  81. set.seed(123)
  82. fit_knn <- train(Ratio ~ Age+AL+ACD+K1+K2+WTW+LT+Ref,data=train.data, method="knn",
  83. metric=metric, preProc=c("center", "scale"),tuneLength=15, trControl=control)
  84. fit_knn
  85. #MARS
  86. set.seed(123)
  87. fit_MARS<- train(Ratio ~ Age+AL+ACD+K1+K2+WTW+LT+Ref,data=train.data,
  88. method="earth", metric=metric, preProc=c("center", "scale"), tuneLength=15, trControl=control)
  89. fit_MARS
  90. # GBM
  91. set.seed(123)
  92. fit_gbm <- train(Ratio ~ Age+AL+ACD+K1+K2+WTW+LT+Ref, data = train.data, method =
  93. "gbm",metric=metric,preProc=c("center", "scale"), tuneLength=15, trControl=control)
  94. fit_gbm
  95. #cubist
  96. set.seed(123)
  97. trControl=control
  98. fit_cubist<- train(Ratio ~ Age+AL+ACD+K1+K2+WTW+LT+Ref, data = train.data, method =
  99. "cubist",metric=metric,preProc=c("center", "scale"), tuneLength=15, trControl=control)
  100. fit_cubist
  101. # Compare algorithms
  102. results <- resamples(list(LM = fit_lm, CART = fit_cart, KNN= fit_knn, RF = fit_rf, SVR = fit_svm,
  103. GBM = fit_gbm, MARS= fit_MARS, Cubist = fit_cubist,ANNs=fit_nnet))
  104. summary(results)
  105. dotplot(results)
  106. # Stacking
  107. library(caretEnsemble)
  108. set.seed(123)
  109. allModels <- caretList(Ratio ~ Age+AL+ACD+K1+K2+WTW+LT+Ref,data=train.data,trControl
  110. = trainControl("repeatedcv", number = 10, repeats = 3, search="random",verboseIter = T),metric =
  111. "RMSE", tuneList = list(cubist = caretModelSpec(method = "cubist", tuneLength = 15, preProc =
  112. c("center","scale")),svmRadial = caretModelSpec(method = "svmRadial", tuneLength = 15, preProc
  113. = c("center","scale")),Earth = caretModelSpec(method = "earth", tuneLength = 15, preProc =
  114. c("center","scale"))))
  115. bwplot(resamples(allModels), metric = "RMSE")
  116. summary(allModels)
  117. # Stack with lm
  118. set.seed(123)
  119. stack <- caretStack(allModels, metric = "RMSE", method = "lm", tuneLength = 10,trControl =
  120. trainControl("repeatedcv", number = 5, repeats = 3, verboseIter = T))
  121. summary(stack)
  122. print(stack)
  123. if(TRUE){
  124. ggplot(varImp(fit_nnet))
  125. pred_Ratio <-predict(stack,test_x)
  126. print(pred_Ratio)
  127. plot(pred_Ratio)
  128. pred_err1 <- test_y$Ratio - pred_Ratio
  129. print(pred_err1)
  130. summary(pred_err1)
  131. plot(pred_err1)
  132. ELPCalc<-function(Ratio,LT,ACD){Ratio*LT+ACD}
  133. ELP = ELPCalc(pred_Ratio,test_x$LT,test_x$ACD)
  134. Pred_Power<-PowerCalc(ELP,test_x$AL,(test_x$K1+test_x$K2)/2,test_x$Ref)
  135. Pred_err2<-test_x$IOLPower - Pred_Power
  136. summary(Pred_err2)
  137. plot(Pred_err2)
  138. }

复现论文也算勉强成功,但是样本量太少,暂无法比较两种方法的优劣。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/290335
推荐阅读
相关标签
  

闽ICP备14008679号