On 02/12/2013 9:47 AM, Michael Friendly wrote:
I want to generate a collection of probability distributions in a data
frame, with
varying parameters. There must be some simpler way than what I have below
(avoiding rbind and cbind), but I can't quite see it.
x <- seq(0,12)
bin.df <- as.data.frame(
rbind( cbind(x, prob=dbinom(x,12,1/6), p=1/6),
cbind(x, prob=dbinom(x,12,1/3), p=1/3),
cbind(x, prob=dbinom(x,12,1/2), p=1/2),
cbind(x, prob=dbinom(x,12,2/3), p=2/3)
))
bin.df$p <- factor(bin.df$p, labels=c("1/6", "1/3", "1/2", "2/3"))
str(bin.df)
> str(bin.df)
'data.frame': 52 obs. of 3 variables:
$ x : num 0 1 2 3 4 5 6 7 8 9 ...
$ prob: num 0.1122 0.2692 0.2961 0.1974 0.0888 ...
$ p : Factor w/ 4 levels "1/6","1/3","1/2",..: 1 1 1 1 1 1 1 1 1 1 ...
>
dbinom can take vector inputs for the parameters, so this would be a bit
simpler:
x <- seq(0,12)
x <- rep(x, 4)
p <- rep(c(1/6, 1/3, 1/2, 2/3), each=13)
bin.df <- data.frame(x, prob = dbinom(x, 12, p), p)
Duncan Murdoch
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.