【发布时间】:2021-11-25 21:35:05
【问题描述】:
我正在尝试创建一个 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))
}
然后在编写 UI 和服务器代码后,我尝试生成一个图表,但它显示错误:“参数意味着不同的行数:0、1”。我测试了pred_prob_func 会产生该错误。虽然当我尝试通过创建变量的随机值列表并尝试查看pred_prob_func(input) 是否产生一个值来创建一个名为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"))
})
我如何定义函数有问题吗?
编辑:
所有文件(数据、RDS 模型)和应用程序都可以在此Google Drive 中找到以进行测试。
我已经使用以下示例输入测试了函数(当我在函数中使用 this 作为参数时会产生结果,但在尝试运行应用程序时会显示此错误):
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)
【问题讨论】:
-
如果我可以提供更多信息,请告诉我,我可以编辑问题。
-
您是否尝试确定错误发生的确切位置?我认为它可能在
test.data.function的cbind函数中。您应该在此函数中添加一些print(input$Age)等...以确保您的输入确实具有值。 -
@gdevaux 我已经使用编辑中的输入测试了函数。
标签: r shiny r-caret caret glmnet