如何在R中使用来自数据帧的参数并通过保持它们的时间循环(How to use parameters from data frame in R and loop through time holding

编程入门 行业动态 更新时间:2024-10-20 03:12:30
如何在R中使用来自数据帧的参数并通过保持它们的时间循环(How to use parameters from data frame in R and loop through time holding them constant)

我有一个函数(weisurv)有2个参数 - sc和shp。 它是通过时间(t)的函数。 时间是一个序列,即t <-seq(1:100)。

weisurv<-function(t,sc,shp){ surv<-exp(-(t/sc)^shp) return(surv) }

我有一个数据框(df),其中包含sc和shp值的列表(如300+)。 例如,我有:

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 ...

我想将sc和shp参数的每一组(ROW)应用到我的函数中。 因此,对于每一行[i],函数看起来像weisurv(t,sc [[i]],shp [i]]。 我不明白如何使用apply或adply来做到这一点虽然我确定其中一个或两者的组合都是需要的。 最后,我正在寻找一个数据框,给出一组sc和shp(保持不变的时间),每次给出weisurv的值。 因此,如果我有10组sc和shp参数,我最终会得到10个weisurv时间序列。 谢谢....

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....

最满意答案

使用plyr:

作为矩阵(cols中的时间,对应于df行的行):

aaply(df, 1, function(x) weisurv(t, x$sc, x$shp), .expand = FALSE)

作为清单:

alply(df, 1, function(x) weisurv(t, x$sc, x$shp))

作为数据框架(按照上面的矩阵结构):

adply(df, 1, function(x) setNames(weisurv(t, x$sc, x$shp), t))

作为长数据帧(每t / sc / shp组合一行); 注意使用mutate和dplyr的管道运算符):

newDf <- data.frame(t = rep(t, nrow(df)), sc = df$sc, shp = df$shp) %>% mutate(surv = weisurv(t, sc, shp))

您还可以创建一个宽的data.frame,然后使用reshape2::melt重新格式化:

wideDf <- adply(df, 1, function(x) setNames(weisurv(t, x$sc, x$shp), t)) newDf <- melt(wideDf, id.vars = colnames(df), variable.name = "t", value.name = "surv") newDf$t <- as.numeric(as.character(newDf$t))

上一个newDf的漂亮情节(使用ggplot2):

ggplot(newDf, aes(x = t, y = surv, col = sprintf("sc = %0.3f, shp = %0.3f", sc, shp))) + geom_line() + scale_color_discrete(name = "Parameters")

weisurv输出的ggplot图像

Using plyr:

As a matrix (time in cols, rows corresponding to rows of df):

aaply(df, 1, function(x) weisurv(t, x$sc, x$shp), .expand = FALSE)

As a list:

alply(df, 1, function(x) weisurv(t, x$sc, x$shp))

As a data frame (structure as per matrix above):

adply(df, 1, function(x) setNames(weisurv(t, x$sc, x$shp), t))

As a long data frame (one row per t/sc/shp combination); note uses mutate and the pipe operator from dplyr):

newDf <- data.frame(t = rep(t, nrow(df)), sc = df$sc, shp = df$shp) %>% mutate(surv = weisurv(t, sc, shp))

You can also create a wide data.frame and then use reshape2::melt to reformat as long:

wideDf <- adply(df, 1, function(x) setNames(weisurv(t, x$sc, x$shp), t)) newDf <- melt(wideDf, id.vars = colnames(df), variable.name = "t", value.name = "surv") newDf$t <- as.numeric(as.character(newDf$t))

Pretty plot of last newDf (using ggplot2):

ggplot(newDf, aes(x = t, y = surv, col = sprintf("sc = %0.3f, shp = %0.3f", sc, shp))) + geom_line() + scale_color_discrete(name = "Parameters")

ggplot image of weisurv output

更多推荐

本文发布于:2023-04-29 12:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336334.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数   时间   数据   如何在   parameters

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!