## from summary data just calculate
## Monte-Carlo and propagated error.
EXPR <- expression(x/y)
x <- c(5, 0.1)
y <- c(1, 0.01)
DF <- cbind(x, y)
res <- propagate(expr = EXPR, data = DF, type = "stat")
## Do Shapiro-Wilks test on Monte Carlo errors
## !maximum 5000 datapoints can be used!
## => p.value indicates normality
shapiro.test(res$eval.Sim[1:5000])
## How about a graphical analysis:
qqnorm(res$eval.Sim)
## using raw data
## bring all vectors to same
## length with NA's
EXPR <- expression(x*y)
x <- c(2, 2.1, 2.2, 2, 2.3, 2.1)
y <- c(4, 4, 3.8, 4.1, NA, NA)
DF <- cbind(x, y)
res <- propagate(EXPR, DF, type = "raw")
## For replicate data, using relative
## quantification ratio from qPCR.
## How good is the estimation of the propagated error?
## Done without using covariance in the
## calculation and simulation.
## STRONG deviation from normality!
EXPR <- expression((E1^cp1)/(E2^cp2))
E1 <- c(1.73, 1.75, 1.77)
cp1 <- c(25.77,26.14,26.33)
E2 <- c(1.72,1.68,1.65)
cp2 <- c(33.84,34.04,33.33)
DF <- cbind(E1, cp1, E2, cp2)
res <- propagate(EXPR, DF, type = "raw", do.sim = TRUE)
par(ask = TRUE)
shapiro.test(res$eval.Sim[1:5000])
qqnorm(res$eval.Sim)
## same setup as above but also
## using a permutation approach
## for resampling confidence interval
## Similar to what REST software will do!
## Scenario 1: just swapping the
## threshold cycles
res <- propagate(EXPR, DF, type = "raw", do.sim = TRUE,
do.perm = TRUE, ties = c(NA, 1, NA, 1))
## 95% confidence interval for the ratio
res$conf.Perm
## Using covariances in calculation and simulation
res2 <- propagate(EXPR, DF, type = "raw",
use.cov = TRUE, do.sim = TRUE)
## Proof that covariance of Monte-Carlo
## simulated dataset is the same as from
## initial data
res2$covMat
cov(res2$data.Sim)
## Error propagation in a linear model
## using the covariance matrix from summary.lm
## Estimate error of y for x = 7
x <- 1:10
set.seed(123)
y <- x + rnorm(10, 0, 1) ##generate random data
mod <- lm(y ~ x)
summ <- summary(mod)
## make matrix of parameter estimates and standard error
DF <- t(coef(summ)[, 1:2])
colnames(DF) <- c("b", "m")
CM <- vcov(mod) ## take var-cov matrix
colnames(CM) <- c("b", "m")
propagate(expression(m*7 + b), DF, type = "stat", use.cov = CM)
## In a x/y regime, when does the propagated error start to
## deviate from normality if error of denominator increases?
## Watch increasing skewness of histogram!
x <- c(5, 1)
for (i in seq(0.01, 0.5, by = 0.01)) {
y <- c(1, i)
DF <- cbind(x, y)
res <- propagate(expression(x/y), DF, type = "stat",
do.sim = TRUE, plot = FALSE)
hist(res$data.Sim, nclass = 100, main = paste("sd(y):",i))
}
Run the code above in your browser using DataLab