## 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 evaluations
## !maximum 5000 datapoints can be used!
## => p.value indicates normality
shapiro.test(res$data.Sim[, 3][1:5000])
## How about a graphical analysis:
qqnorm(res$data.Sim[, 3])
## Using raw data
## bring all vectors to same
## length with NA's.
## Do permutations (swap x and y values)
EXPR <- expression(x*y)
x <- c(2, 2.1, 2.2, 2, 2.3, 2.1)
y <- c(4, 4, 3.8, 4.1, 3.1, 3)
DF <- cbind(x, y)
res <- propagate(EXPR, DF, type = "raw", do.perm = TRUE)
## Have a look at permutation result
res$data.Perm
## 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!
## cp's and efficiencies are tied together
## because they are two observations on the
## same sample!
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,
do.perm = FALSE)
par(ask = TRUE)
shapiro.test(res$data.Sim[, 3][1:5000])
qqnorm(res$data.Sim[, 3])
## Same setup as above but also
## using a permutation approach
## for resampling confidence interval
## Cp's and efficiencies are tied together
## because they are two observations on the
## same sample!
## Similar to what REST2008 software does.
res <- propagate(EXPR, DF, type = "raw", do.sim = TRUE,
perm.crit = "perm < init", do.perm = TRUE,
ties = c(1, 1, 2, 2))
## 95\% confidence interval for the ratio
res$conf.Perm
## Using covariances in calculation and simulation
res2 <- propagate(EXPR, DF, type = "raw", do.sim = TRUE,
perm.crit = "perm < init", do.perm = TRUE,
ties = c(1, 2, 1, 2), use.cov = 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[, 3], nclass = 100, main = paste("sd(y):",i))
}
Run the code above in your browser using DataLab