Forgive me if this question is basic, but I can't seem to find a similar example. I am relatively new to R and creating function. I have my code working without a function, but would like to convert it to one to be used in Shiny
I am trying to develop a function to reflect the duration of parasite treatment at a given time (this is part of a larger model). The output is a dataframe (called signal) where X = time and Y= the % of parasites killed at that time (labeled import).
I want the function to allow the user to input the 1)start time of treatment 2) the frequency of treatment 3) the length of time between each treatment. So for example, the user could set the treatment to start at 10000 days, to be given 4 times, 3 months apart. I have this coded without a function like so:
#Initial vectors for days post treatment, % killed
x <- c(4, 30, 60, 90, 120, 210, 360) #days post treatment
z <- c(1.0, 0.99, 0.99, 0.79, 0.7, 0.02, 0) #% killed
#=============================================
# fit data with logistic curve
# -plot to check fit
# -extract fit values using equation y = Asym / (1 + exp((xmid - input) / scal))
#=============================================
fit2 <- nls(z ~ SSlogis(x, Asym, xmid, scal), data = data.frame(x, z))
summary(fit2)
lines(seq(0, 400, length.out = 400),
predict(fit2, newdata = data.frame(x = seq(0.5, 400, length.out = 400))))
Asym<-summary(fit2)$parameters[1,1]
xmid<-summary(fit2)$parameters[2,1]
scal<-summary(fit2)$parameters[3,1]
#=============================================
# -create data frame "signal" that holds times and import (% killed at given time based on above)
# -model population from additional code shows population doesn't reach equilibrium until 10000 #
# days, so start treatment after
# This is where function would need to begin but using input from above
times <- seq(0, 20000, by = 1)
signal <- data.frame(times = times, import = rep(0, length(times)))
#Example of giving one treatment:
# Treatment apply at 10000 days
signal$import[c(10000:20001)] = (Asym / (1 + exp((xmid - times[0:10002]) / scal))
#==========GIVING 4 TREATMENTS, 3 MONTHS APART, STARTING AT 10000 DAYS===============#
signal$import[c(10000:10400, 10090:10490, 10108:10508, 10198:10598)] = (Asym / (1 + exp((xmid - times[c(0:401, 0:401, 0:401, 0:401)]) / scal)))
# Treatment 1 at 10000 days
# Treatment 2 at 10120 days
# Treatment 3 at 10240 days
#Plot the curve to verify the parasite treatment wearing off over time:
(plot(times,signal$import, xlim = c(9900, 14000),
main="(Asym / (1 + exp((xmid - times)", cex.main=0.8, cex=0.5))
I've been coding this by hand to make sure the lengths of the import and time vectors are equivalent but I would like to be able to create a function so this could be put into a Shiny app (with the rest of the model). The plot is not important to what I need, but I thought it would be helpful to illustrate what I'm doing.
If anybody has some suggestions of examples I could work off of or helpful links I could check out I would really appreciate it! Thanks!