Bla*_*aan 5 r caret glmnet shiny r-caret
我正在尝试创建一个 R Shiny 应用程序,它使用岭回归计算分数,然后在随机森林模型中使用它。我将这两个模型保存为 RDS,并将它们保存在 app.R 所在的同一文件夹中。
然后我读取预测概率的模型和数据并定义一些函数:
# Reading data
phats <- read.csv("Predicted_probabilities_training.csv")
phats_graph <- phats %>% mutate(`PTD Event Status` = ifelse(Observed_Event=="PTD", "PTD Event", "No PTD Event"))
phats_event <- phats[phats$Observed_Event=="PTD",]
phats_nonevent <- phats[phats$Observed_Event=="No_PTD",]
# load the models
ridge_model <- readRDS("ridge_model.rds")
final_model <- readRDS("final_model.rds")
# Defining some functions
matrix.for.ILS.function <- function(input){
return(data.matrix(data.frame(input$MIP_1a,
input$MIP_3a,
input$RANTES,
input$sIL_6R,
input$ITAC,
input$IL_21,
input$Fractalkine,
input$TNF_a,
input$IL_1b,
input$IL_7,
input$IL_10,
input$GM_CSF,
input$MIP_1b)))
}
ILS.function <- function(input){
ILS.ridge = as.numeric(predict(ridge_model, matrix.for.ILS.function(input), s = 0.7414409, type="link"))
return(ILS.ridge)
}
test.data.function <- function(input){
test.data = data.frame(Age=input$Age,
Gender=input$Gender,
GCS_Bestin24=input$GCS_Bestin24,
Premorbid_depression=input$Premorbid_depression,
Antidep_first6m=input$Antidep_first6m)
return(cbind(test.data, ILS.ridge=ILS.function(input)))
}
pred_prob_func <- function(input){
pred_prob = predict(final_model, test.data.function(input), type="prob")[,"PTD"]
classification <- ifelse(pred_prob >= input$thresholdslider, "PTD Event", "No Event")
return(list(pred_prob=pred_prob, classification=classification))
}
Run Code Online (Sandbox Code Playgroud)
然后,在编写 UI 和服务器代码后,我尝试生成一个图表,但它显示错误:“参数意味着不同的行数:0, 1”。我测试过pred_prob_func会产生该错误。尽管当我尝试创建一个input仅通过为变量创建随机值列表来调用的列表并尝试查看是否pred_prob_func(input)产生一个值时,它实际上产生了一个值。但是当我尝试运行该应用程序时,图表没有显示并给出了此错误。我在服务器部分使用以下代码来生成图表。
output$int_plot <- renderPlotly({
phats_graph$`Total Percentile` <- unlist(t(sapply(phats_graph$Phat, function(x) quantile_fun(value=x)))[,1])
phats_graph$`PTD Percentile` <- unlist(t(sapply(phats_graph$Phat, function(x) quantile_fun(value=x)))[,2])
phats_graph$`No PTD Percentile` <- unlist(t(sapply(phats_graph$Phat, function(x) quantile_fun(value=x)))[,3])
int_plot <- ggplot(phats_graph, aes(ptd_per=`No PTD Percentile`)) + geom_density(aes(x=Phat, fill = `PTD Event Status`), alpha=0.5) +
geom_vline(xintercept = input$thresholdslider, linetype = 'dashed') +
geom_vline(xintercept = pred_prob_func(input)$pred_prob) +
xlab('Threshold Percentage') + ylab('Density') +
theme_minimal() + scale_fill_manual(values=c("#5D3A9B", "#E66100"), name="")
ggplotly(int_plot, tooltip=c("x", "ptd_per"))
})
Run Code Online (Sandbox Code Playgroud)
我定义函数的方式有问题吗?
编辑:
所有文件(数据、RDS 模型)和应用程序都可以在此Google Drive中找到以进行测试。
我已经使用以下示例输入测试了这些函数(当我在函数中使用它作为参数时产生结果,但当我尝试运行应用程序时显示此错误):
input=list(Age=20, Gender=1, GCS_Bestin24=4, Premorbid_depression=0, Antidep_first6m=0,
MIP_1a=1,
MIP_3a=1,
RANTES=1,
sIL_6R=1,
ITAC=1,
IL_21=1,
Fractalkine=1,
TNF_a=1,
IL_1b=1,
IL_7=1,
IL_10=1,
GM_CSF=1,
MIP_1b=1,
thresholdslider=0.5)
Run Code Online (Sandbox Code Playgroud)
发生原始错误是因为您没有要求input$id与id您在 中分配的相同的内容textInput。
textInput("MIPa", label = HTML("MIP-1\\(\\alpha\\)") ##used in ui
input$MIP_1a ##used in server.
Run Code Online (Sandbox Code Playgroud)
我认为使用闪亮的变量有问题input。
通过创建输入列表并更改您自己input_list调用的位置,它似乎可以工作。inputinput()
test.data.function(input)我还需要在创建函数后删除右侧test.data.function()以使应用程序运行。
input_list <- reactive({
input_list <- list( Age=as.numeric(input$Age),
Gender=ifelse(input$Gender == "Female", 1, 0),
GCS_Bestin24=as.numeric(input$GCS_Bestin24),
Premorbid_depression=as.numeric(input$Premorbid_depression),
Antidep_first6m=as.numeric(input$Antidep_first6m),
MIP_1a = as.numeric(input$MIPa),
MIP_3a = as.numeric(input$MIP_3a),
RANTES = as.numeric(input$RANTES),
sIL_6R = as.numeric(input$sIL_6R),
ITAC = as.numeric(input$ITAC),
IL_21 = as.numeric(input$IL_21),
Fractalkine = as.numeric(input$Fractalkine),
TNF_a = as.numeric(input$TNF_a),
IL_1b = as.numeric(input$ILb),
IL_7 = as.numeric(input$IL_7),
IL_10 = as.numeric(input$IL0),
GM_CSF = as.numeric(input$GM_CSF),
MIP_1b = as.numeric(input$MIPb))
input_list
})
Run Code Online (Sandbox Code Playgroud)
##input changed to input_list()
##this needs to be done for each instance where input is used on its own.
geom_vline(xintercept = pred_prob_func(input_list())$pred_prob)
Run Code Online (Sandbox Code Playgroud)
要删除最终的错误消息,您需要将 更改renderPlotly()为并renderUI()检查.NAinput_list
还plotlyOutput需要更改为部分uiOutput()中的ui。
ui <- fluidPage(
uiOutput('int_plot') #changed from plotlyOutput('int_plot')
)
server <- (
output$int_plot <- renderUI({
if(anyNA(input_list())){
helpText("Input User data")
}else{
phats_graph$`Total Percentile` <- unlist(t(sapply(phats_graph$Phat, function(x) quantile_fun(value=x)))[,1])
phats_graph$`PTD Percentile` <- unlist(t(sapply(phats_graph$Phat, function(x) quantile_fun(value=x)))[,2])
phats_graph$`No PTD Percentile` <- unlist(t(sapply(phats_graph$Phat, function(x) quantile_fun(value=x)))[,3])
int_plot <- ggplot(phats_graph, aes(ptd_per=`No PTD Percentile`)) + geom_density(aes(x=Phat, fill = `PTD Event Status`), alpha=0.5) +
geom_vline(xintercept = input$thresholdslider, linetype = 'dashed') +
geom_vline(xintercept = pred_prob_func(input_list())$pred_prob) +
xlab('Threshold Percentage') + ylab('Density') +
theme_minimal() + scale_fill_manual(values=c("#5D3A9B", "#E66100"), name="")
renderPlotly( ggplotly(int_plot, tooltip=c("x", "ptd_per")))
}
})
)
Run Code Online (Sandbox Code Playgroud)
当年龄为 10 且所有值均为 1(不知道这些值应该是什么)时,输出如下:
