I have a function (weisurv) that has 2 parameters - sc and shp. It is a function through time (t). Time is a sequence, i.e. t<-seq(1:100).
weisurv<-function(t,sc,shp){
surv<-exp(-(t/sc)^shp)
return(surv)
}
I have a data frame (df) that contains a list of sc and shp values (like 300+ of them). For example, I have:
M shp sc p C i
1 1 1.138131 10.592154 0.1 1 1
2 1.01 1.143798 10.313217 0.1 1 2
3 1.02 1.160653 10.207863 0.1 1 3
4 1.03 1.185886 9.861997 0.1 1 4
...
I want to apply each set (ROW) of sc and shp parameters to my function. So the function would look like weisurv(t,sc[[i]],shp[i]]) for each row[i]. I do not understand how to use apply or adply to do this though I'm sure one of these or a combo of both are what is needed. In the end, I am looking for a data frame that gives a value of weisurv for each time given a set of sc and shp (held constant through time). So if I had 10 sets of sc and shp parameters, I would end up with 10 time series of weisurv. Thanks....
Using plyr:
As a matrix (time in cols, rows corresponding to rows of df):
As a list:
As a data frame (structure as per matrix above):
As a long data frame (one row per t/sc/shp combination); note uses mutate and the pipe operator from
dplyr
):You can also create a wide data.frame and then use
reshape2::melt
to reformat as long:Pretty plot of last newDf (using ggplot2):