Skip to content

Commit 7e2b91f

Browse files
committed
minor updates
1 parent 306456e commit 7e2b91f

9 files changed

+90
-14
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ DHARMa_0.0.0.9000.tgz
1717
DHARMa/Rplots.pdf
1818

1919
DHARMa/vignettes/Demo.html
20+
21+
*.gz

DHARMa/DESCRIPTION

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
Package: DHARMa
22
Title: Residual Diagnostics for HierArchical (Multi-level / Mixed) Regession Models
3-
Version: 0.0.0.9000
3+
Version: 0.0.1.0
4+
Date: 16.6.2016
45
Authors@R: c(person("Florian", "Hartig", email = "florian.hartig@biom.uni-freiburg.de", role = c("aut", "cre")))
56
Author: Florian Hartig [aut, cre],
67
Maintainer: Florian Hartig <florian.hartig@biom.uni-freiburg.d>
78
Description: The package creates easily interpretable, residuals for generalized linear mixed models that are standardized to values between 0 and 1. This is achieved by a simulation-based approach, similar to the Bayesian p-value or the parametric bootstrap: 1) simulate new data from the fitted model 2) calculate the cummulative empirical density function 3) residual is the value of the empirical density function at the value of the observed data.
89
Depends:
910
R (>= 3.1.2)
1011
Imports:
11-
gap
12+
gap,
13+
quantreg,
14+
plyr
1215
Suggests:
1316
lme4
1417
License: What license is it under?

DHARMa/R/createData.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#' Function to simulate data
22
#' @export
33
#' @param overdispersion if this is a numeric value, it will be used as the sd of a random normal variate that is added to the linear predictor. Alternatively, a random function can be provided that takes as input the linear predictor.
4-
#' @example /inst/examples/createPrior.R
4+
#' @example /inst/examples/createDataHelp.R
55
createData <- function(replicates=1, sampleSize = 10, intercept = 0, fixedEffects = 1, numGroups = 10, randomEffectVariance = 1, overdispersion = 0.5, family = poisson(), scale = 1, cor = 0, roundPoissonVariance = NULL, quadraticFixedEffects = NULL){
66

77
nPredictors = length(fixedEffects)
@@ -39,7 +39,7 @@ createData <- function(replicates=1, sampleSize = 10, intercept = 0, fixedEffect
3939
else if (family$family == "binomial") observedResponse = rbinom(n = sampleSize, 1, prob = linkResponse)
4040
else if (family$family == "poisson") {
4141
if(is.null(roundPoissonVariance)) observedResponse = rpois(n = sampleSize, lambda = linkResponse)
42-
else observedResponse = round(rnorm(n = length(linkResponse), mean = linkResponse, sd = sqrt(linkResponse) * roundPoissonVariance))
42+
else observedResponse = round(rnorm(n = length(linkResponse), mean = linkResponse, sd = roundPoissonVariance))
4343
}
4444

4545
else stop("wrong link argument supplied")

DHARMa/R/plotSimulatedResiduals.R

+55-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,68 @@
11
#' creates Poisson data overdispersion and random intercept
2-
plotSimulatedResiduals <- function(simulationOutput){
2+
plotSimulatedResiduals <- function(simulationOutput, quantreg = F){
3+
4+
5+
pred = simulationOutput$fittedPredictedResponse
6+
7+
resid = simulationOutput$scaledResiduals
8+
9+
dat = data.frame(pred, resid)
10+
11+
312
par(mfrow = c(1,2), oma = c(0,1,2,1))
413

5-
gap::qqunif(simulationOutput$scaledResiduals,pch=21,bg="blue",bty="n", logscale = F)
14+
gap::qqunif(simulationOutput$scaledResiduals,pch=2,bty="n", logscale = F, col = "black", cex = 0.6, main = "QQ plot residuals", cex.main = 1)
615
#hist(simulationOutput$scaledResiduals, main = "Distribution of scaled residuals", breaks = 50, freq = F)
716
#lines(density(simulationOutput$scaledResiduals, na.rm = T, from = 0, to = 1, kernel = "rectangular", bw = 0.01, cut = 0.01), col = "red")
817

9-
plot(simulationOutput$fittedPredictedResponse, simulationOutput$scaledResiduals, xlab = "predicted", ylab = "Residual", main = "Residual vs. predicted")
18+
plot(simulationOutput$fittedPredictedResponse, simulationOutput$scaledResiduals, xlab = "Predicted", ylab = "Residual", main = "Residual vs. predicted\n lines should match", cex.main = 1)
19+
20+
21+
22+
23+
if(quantreg == F){
24+
25+
lines(smooth.spline(simulationOutput$fittedPredictedResponse, simulationOutput$scaledResiduals, df = 10), lty = 2, lwd = 2, col = "red")
26+
27+
abline(h = 0.5, col = "red", lwd = 2)
28+
29+
}else{
30+
31+
require(quantreg)
32+
dat <- plyr::arrange(dat,pred)
33+
fit<-quantreg::rqss(resid~qss(pred,constraint="N"),tau=0.5,data = dat)
34+
lines(unique(dat$pred)[-1],fit$coef[1] + fit$coef[-1], col = "red", lwd = 2)
35+
abline(h = 0.5, col = "red", lwd = 2)
36+
37+
fit<-quantreg::rqss(resid~qss(pred,constraint="N"),tau=0.25,data = dat)
38+
lines(unique(dat$pred)[-1],fit$coef[1] + fit$coef[-1], col = "green", lwd = 2, lty =2)
39+
abline(h = 0.25, col = "green", lwd = 2, lty =2)
40+
41+
fit<-quantreg::rqss(resid~qss(pred,constraint="N"),tau=0.75,data = dat)
42+
lines(unique(dat$pred)[-1],fit$coef[1] + fit$coef[-1], col = "blue", lwd = 2, lty = 2)
43+
abline(h = 0.75, col = "blue", lwd = 2, lty =2)
44+
}
45+
46+
47+
1048

1149
mtext("DHARMa scaled residual plots", outer = T)
1250
}
1351

52+
#' Plots a generic residual plot with spline
53+
plotResiduals <- function(pred, res){
54+
55+
plot(pred, res)
56+
57+
lines(smooth.spline(simulationOutput$fittedPredictedResponse, simulationOutput$scaledResiduals, df = 10), lty = 2, lwd = 2, col = "red")
58+
59+
abline(h = 0.5, col = "red", lwd = 2)
60+
61+
}
62+
63+
64+
65+
#plotSimulatedResiduals(simulationOutput)
1466

1567
#plot(simulationOutput$observedResponse, simulationOutput$scaledResiduals, xlab = "predicted", ylab = "Residual", main = "Residual vs. predicted")
1668

DHARMa/man/createData.Rd

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

DHARMa/man/plotResiduals.Rd

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

DHARMa/man/plotSimulatedResiduals.Rd

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

DHARMa/vignettes/Demo.Rmd

+7-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ vignette: >
99
---
1010

1111
```{r global_options, include=FALSE}
12-
knitr::opts_chunk$set(fig.width=7, fig.height=4, warning=FALSE, message=FALSE, cache = T)
12+
knitr::opts_chunk$set(fig.width=8, fig.height=5, warning=FALSE, message=FALSE, cache = F)
1313
```
1414

1515
***Summary**: The DHARMa package creates easily interpretable, residuals for generalized linear mixed models that are standardized to values between 0 and 1. This is achieved by a simulation-based approach, similar to the Bayesian p-value or the parametric bootstrap: 1) simulate new data from the fitted model 2) calculate the cummulative empirical density function 3) residual is the value of the empirical density function at the value of the observed data.*
@@ -61,7 +61,7 @@ The above exampel shows a misspecified model. The reason is that we created data
6161
For comparison, also "standard residual plots" (deviance, pearso and raw) are shown
6262

6363
```{r}
64-
testData = createData(sampleSize = 500, intercept = 0, overdispersion = 0, family = poisson(), randomEffectVariance = 0)
64+
testData = createData(sampleSize = 250, intercept = 0, overdispersion = 0, family = poisson(), randomEffectVariance = 0)
6565
fittedModel <- glmer(observedResponse ~ Environment1 + (1|group) , family = "poisson", data = testData)
6666
6767
plotConcentionalResiduals(fittedModel)
@@ -105,9 +105,11 @@ plotSimulatedResiduals(simulationOutput = simulationOutput)
105105

106106

107107
```{r}
108-
testData = createData(sampleSize = 500, intercept=1, fixedEffects = 2, overdispersion = 0, family = poisson(), roundPoissonVariance = 0.6)
108+
testData = createData(sampleSize = 500, intercept=0, fixedEffects = 2, overdispersion = 0, family = poisson(), roundPoissonVariance = 0.001, randomEffectVariance = 0)
109109
fittedModel <- glmer(observedResponse ~ Environment1 + (1|group) , family = "poisson", data = testData)
110110
111+
summary(fittedModel)
112+
111113
plotConcentionalResiduals(fittedModel)
112114
113115
simulationOutput <- simulateResiduals(fittedModel = fittedModel)
@@ -173,8 +175,8 @@ Difficult to see with the overall pattern, but it becomes clear if we plot again
173175

174176
```{r}
175177
par(mfrow = c(1,2))
176-
plot(testData$Environment1, simulationOutput$scaledResiduals)
177-
plot(testData$Environment2, simulationOutput$scaledResiduals)
178+
plotResiduals(testData$Environment1, simulationOutput$scaledResiduals)
179+
plotResiduals(testData$Environment2, simulationOutput$scaledResiduals)
178180
```
179181

180182

copyDropbox.command

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
rsync -avz --delete /Users/Florian/Home/Projekte/Papers_in_Progress/15-RegressionDiagnosticsGLMM/DHARMa/DHARMa_0.0.0.9000.tar.gz /Users/Florian/Dropbox/Deploy/DHARMa/DHARMa_0.0.0.9000.tar.gz
1+
rsync -avz --delete /Users/Florian/Home/Projekte/Papers_in_Progress/15-RegressionDiagnosticsGLMM/DHARMa/DHARMa_0.0.* /Users/Florian/Dropbox/Deploy/DHARMa/DHARMa_0.0.0.9000.tar.gz
22

0 commit comments

Comments
 (0)