I'm trying to fit some non-linear models in shiny to identify the best parameter values and then use them as initial values. As an example, I'm using a non-linear logistic model.
Below is the computational routine:
library(shiny)
library(ggplot2)
ui <- fluidPage(
sliderInput("beta_0", "Beta 0:", min = -100, max = 100, value = 1),
sliderInput("beta_1", "Beta 1:", min = -1000, max = 100, value = 1),
sliderInput("beta_2", "Beta 2:", min = -100, max = 100, value = 1),
plotOutput("grafico")
)
server <- function(input, output) {
logistic_model <- function(x, beta_0, beta_1, beta_2) {
return (beta_0 / (1 + beta_1 * exp(-beta_2 * x)))
}
data_modelling <- structure(list(x = c("10.66", "16.87", "12.57", "15.92", "9.71",
"15.92", "17.35", "6.37", "11.94", "11.14", "8.91", "13.05",
"17.67", "10.66", "17.19", "7", "10.82", "11.62", "16.71", "18.3",
"11.78", "12.25", "8.91", "10.98", "17.03", "15.92", "12.73",
"12.41", "11.78", "18.62"), y = c("15.2", "18.3", "15.7", "17.5",
"14.8", "16.7", "19.8", "10.3", "18.6", "14.4", "14.3", "17.8",
"21", "18.8", "18.9", "13.7", "17.6", "17.5", "19.6", "18.8",
"15.3", "16.8", "15.1", "14.7", "18.5", "17.9", "17.8", "16.9",
"17.5", "18.7")), row.names = 31:60, class = "data.frame")
data_modelling$x = as.numeric(data_modelling$x)
data_modelling$y = as.numeric(data_modelling$y)
output$grafico <- renderPlot({
ggplot(data_modelling, aes(x = x, y = y)) +
geom_point() +
stat_function(fun = logistic_model, args = list(
beta_0 = input$beta_0,
beta_1 = input$beta_1,
beta_2 = input$beta_2
)) +
labs(x = "X", y = "Y")
})
}
shinyApp(ui, server)
The problem I'm having is that the curve doesn't appear on top of the actual sigmoid-shaped data. I've tried numerous parameters and my model is correct (the model considered was based on this article: 10.34188/bjaerv4n1-035), see: