knitr::opts_chunk$set(comment='.')
Mutate a data.frame
, adding random variates.
library(dplyr) library(dmutate)
Some variables to use in formulae:
low_wt <- 70 high_wt <- 90 mu_wt <- 80 sd <- 60 p.female <- 0.24
Use mutate_random
to implement formulae in data frame. We can put bounds on any simulated variable
data.frame(ID=1:10) %>% mutate_random(WT[low_wt,high_wt] ~ rnorm(mu_wt,sd))
We can simulate from any probability distirbution in R
data.frame(ID=1:10) %>% mutate_random(X ~ rcauchy(0,0.5))
We can add the variate at any level
data.frame(ID=1:10) %>% mutate(GROUP = ID%%2) %>% mutate_random(STUDY_RE ~ rnorm(50,sqrt(50))|GROUP)
mu <- c(2,200) Sigma <- diag(c(10,1000)) XY <- X[0,] + Y[200,300] ~ rmvnorm(mu,Sigma)
The object
XY
Simulate
data.frame(ID=1:10000) %>% mutate_random(XY) %>% summary
data.frame(ID=1:10) %>% mutate(GROUP = ID%%2) %>% mutate_random(WT[low_wt,high_wt] ~ rnorm(mu_wt,1)) %>% mutate_random(STUDY_RE ~ rnorm(0,sqrt(50))|GROUP) %>% mutate_random(SEX ~ rbinomial(p.female)) %>% mutate_random(sigma ~ rgamma(1,1)) %>% mutate_random(kappa ~ rgamma(1,1)|GROUP) %>% signif(3)
expr
to calculate new columns in the data.frame
using dplyr::mutate
We can easily save formulae to R
variables. We collect formulae together into sets called covset
. For better control for
where objects are found, we can specify an environment where objects can be found.
a <- X ~ rnorm(50,3) b <- Y ~ expr(X/2 + c) d <- A+B ~ rlmvnorm(log(c(20,80)),diag(c(0.2,0.2))) cov1 <- covset(a,b,d) e <- list(c=3)
Notice that b
has function expr
. This assigns the column named Y
(in this case) to the result of evaluating the
expression in the data frame using dplyr::dmutate
.
.data <- data.frame(ID=1:3) mutate_random(.data,cov1,envir=e) %>% signif(3)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.