#| '!! shinylive warning !!': |
#| shinylive does not work in self-contained HTML documents.
#| Please set `embed-resources: false` in your metadata.
#| standalone: true
#| viewerHeight: 420
# Load the shiny package
library(shiny)
# Define UI for the application
ui <- fluidPage(
# titlePanel("Plotting a Function of x with Parameters a, b, c, d"),
sidebarLayout(
sidebarPanel(
numericInput("a", "Parameter a:", value = 1),
numericInput("b", "Parameter b:", value = 1),
numericInput("c", "Parameter c:", value = 1),
numericInput("d", "Parameter d:", value = 1)
),
mainPanel(
plotOutput("plot")
)
)
)
# Define server logic
server <- function(input, output) {
output$plot <- renderPlot({
a <- input$a
b <- input$b
c <- input$c
d <- input$d
# Define the sigmoid activation function
sigmoid <- function(x, a=0, b=1) {
1 / (1 + exp(-(a+b*x)))
}
ssigmoid <- function(x, a=0, b=1, c=0, d=1) {
1 / (1 + exp(-(a+b*sigmoid(x, c, d))))
}
x <- seq(-10, 10, length.out = 201)
y <- ssigmoid(x, a, b, c, d)
# yy <- log(y/(1-y))
# co <- coef(lm(yy~x))
loss <- function(par){
mean((y - sigmoid(x, par[1], par[2]))^2)
}
o <- optim(c(0, 1), loss)
co <- o$par
plot(x, y, type = "l", col = "blue", lwd = 2,
xlim = c(-10, 10), ylim = c(0, 1),
xlab = "x", ylab = "sigmoid(a+b*sigmoid(c+d*x))",
main = "")
lines(x, sigmoid(x, co[1], co[2]), col = "red", lty = 2)
})
}
# Run the application
shinyApp(ui = ui, server = server)