Simulating data from a fitted generalised linear binomial model - r

I have asked this question on the r-sig-mixed-models mailing list but have received no helpful answers, so I am trying here. Fingers crossed.
I apologise for the length/prolixity of my question, but the structure is complicated and I see no way to shorten my discussion.
Although this is not relevant to the question I am asking, my ultimate objective is to simulate new data from a fitted model, but with the experimental design changed from that of the
data set to which the model was fitted. The changes involve altering the number of replicates (replicates being the underlying random effect) and altering the number of binomial trials associated with each observation. The final goal is to examine, via simulation, the relative impact of the number of replicates and the number of binomial trials on the precision with which a certain (intricate) parameter is estimated.
I am working with the lme4 R package.
I had hoped to be able to adjust the experimental design from which the simulated data are generated by means of the "newdata" argument of the function simulate.merMod(), but I cannot get this to work. I am probably misunderstanding something. I am too slow and stupid to follow the help for simulate.merMod().
Since I could not accomplish my aim with simulate.merMod() I decided to try a roll-your-own approach. The problem here is that I need to be sure that this roll-your-own approach is correct. As a check, I tried comparing the roll-your-own results with results from simulate.merMod() (with seeds set appropriately so that the "random" results should be the same).
Basically my roll-your-own procedure consists of constructing a linear predictor using
* fitted fixed effect coefficients from the fitted model
* random effects simulated on the basis of the fitted
random effects variances and covarances
I then form probabilities as the logistic function of the linear
predictor, and finally use rbinom() to generate a random sample
from these probabilities and the desired "sizes".
I get agreement when there is a single random effect in the model, but I cannot get agreement when there are two random effects, and cannot see what I am doing wrong.
For the single random effect setting I made use of the "cbpp" data set from the lme4 package. The code that I used is as follows:
library(lme4)
fit <- glmer(cbind(incidence, size - incidence) ~ 0 + period + (1 | herd),
family = binomial, data = cbpp)
ccc <- getME(fit,"beta")
sigma <- getME(fit,"theta")
# Roll-your-own:
set.seed(101)
Z <- rnorm(length(levels(cbpp$herd)),0,sigma)
lnpr <- with(cbpp,ccc[period] + Z[herd])
p <- 1/(1+exp(-lnpr))
s.ryo <- rbinom(nrow(cbpp),cbpp$size,p)
# Using simulate.merMod:
set.seed(101)
s.mer <- simulate(fit)
s.mer <- s.mer[,1][,1]
# Check for equality:
print(all.equal(s.ryo,s.mer))
The two simulated results agree exactly.
However the setting in which I am really interested involves two random effects (random intercepts and random "slopes", i.e. random coefficients for the numeric predictor). Following is code that I tried to use in this
setting. In this case I cannot get the results from roll-your-own and simulate.merMod() to agree. The data set that I use in this case is not contained in the lme4 package.
This data set is provided after the illustrative code.
Sorry for the rather extreme length of the display.
I would appreciate any advice as to what I am doing wrong,
and comments as to whether my roll-your-own approach makes any sense at all.
Code:
library(lme4)
fit <- glmer(cbind(Good, Bad) ~ (trtmnt+0)/x + (x | batch),
family = binomial, data = Dat) # See below for "Dat"
ccc <- fixef(fit)
Sigma <- VarCorr(fit)[[1]]
# Roll-your-own:
set.seed(101)
nrep <- length(levels(Dat$batch))
Z <- MASS::mvrnorm(nrep,c(0,0),Sigma)
beta0 <- ccc[1:4][as.numeric(Dat$trtmnt)]
beta1 <- ccc[5:8][as.numeric(Dat$trtmnt)]
lnpr <- with(Dat,beta0 + beta1*x + Z[batch,1] + Z[batch,2]*x)
p <- 1/(1+exp(-lnpr)) # Inverse of logit link.
size <- with(Dat,Good+Bad)
s.ryo <- rbinom(nrow(Dat),size,p)
# Using simulate.merMod:
set.seed(101)
s.mer <- simulate(fit)
s.mer <- s.mer[,1][,1]
# The results are not equal.
Data:
Dat <- structure(list(Good = c(87, 137, 194, 211, 250, 259, 272, 277,
279, 279, 279, 279, 76, 134, 216, 229, 253, 264, 275, 282, 286,
287, 287, 287, 90, 109, 209, 219, 228, 245, 240, 244, 247, 247,
247, 247, 113, 147, 230, 257, 283, 287, 290, 295, 298, 298, 298,
298, 60, 105, 175, 203, 237, 250, 263, 268, 267, 268, 268, 268,
32, 71, 163, 184, 206, 220, 231, 242, 243, 245, 245, 245, 104,
138, 254, 265, 261, 284, 282, 283, 285, 286, 286, 286, 57, 89,
155, 193, 210, 214, 219, 229, 231, 231, 231, 231, 42, 71, 136,
154, 197, 211, 226, 228, 229, 229, 229, 229, 47, 69, 140, 167,
208, 215, 235, 241, 244, 246, 246, 246, 49, 79, 138, 179, 198,
213, 214, 220, 220, 221, 221, 221, 57, 85, 170, 186, 224, 221,
231, 232, 234, 235, 235, 235, 57, 92, 162, 185, 219, 234, 240,
243, 242, 243, 243, 243, 55, 95, 178, 205, 237, 255, 263, 274,
274, 276, 278, 278, 53, 81, 183, 236, 243, 279, 285, 290, 288,
291, 291, 291, 72, 101, 166, 209, 223, 219, 238, 236, 238, 238,
238, 238, 49, 77, 135, 171, 182, 195, 205, 211, 213, 214, 214,
214, 214, 28, 63, 123, 144, 160, 182, 187, 198, 201, 203, 204,
205, 205, 36, 64, 169, 183, 190, 206, 211, 213, 216, 217, 218,
218, 218, 58, 76, 163, 182, 194, 198, 204, 203, 207, 207, 208,
208, 208, 41, 82, 145, 163, 195, 213, 221, 226, 228, 229, 229,
229, 229, 42, 66, 121, 153, 179, 187, 203, 214, 216, 216, 218,
218, 218, 57, 71, 132, 185, 190, 198, 205, 207, 209, 210, 210,
210, 210, 43, 68, 132, 174, 186, 190, 199, 206, 206, 208, 208,
208, 208, 38, 65, 115, 126, 178, 190, 203, 211, 212, 213, 213,
213, 213, 34, 66, 124, 144, 183, 199, 224, 227, 232, 234, 235,
235, 235, 37, 81, 132, 166, 182, 190, 212, 218, 218, 220, 220,
220, 220, 42, 73, 129, 180, 207, 214, 242, 245, 247, 247, 248,
248, 248), Bad = c(192, 142, 85, 68, 29, 20, 7, 2, 0, 0, 0, 0,
211, 153, 71, 58, 34, 23, 12, 5, 1, 0, 0, 0, 157, 138, 38, 28,
19, 2, 7, 3, 0, 0, 0, 0, 185, 151, 68, 41, 15, 11, 8, 3, 0, 0,
0, 0, 208, 163, 93, 65, 31, 18, 5, 0, 1, 0, 0, 0, 213, 174, 82,
61, 39, 25, 14, 3, 2, 0, 0, 0, 182, 148, 32, 21, 25, 2, 4, 3,
1, 0, 0, 0, 174, 142, 76, 38, 21, 17, 12, 2, 0, 0, 0, 0, 187,
158, 93, 75, 32, 18, 3, 1, 0, 0, 0, 0, 199, 177, 106, 79, 38,
31, 11, 5, 2, 0, 0, 0, 172, 142, 83, 42, 23, 8, 7, 1, 1, 0, 0,
0, 178, 150, 65, 49, 11, 14, 4, 3, 1, 0, 0, 0, 186, 151, 81,
58, 24, 9, 3, 0, 1, 0, 0, 0, 223, 183, 100, 73, 41, 23, 15, 4,
4, 2, 0, 0, 238, 210, 108, 55, 48, 12, 6, 1, 3, 0, 0, 0, 166,
137, 72, 29, 15, 19, 0, 2, 0, 0, 0, 0, 165, 137, 79, 43, 32,
19, 9, 3, 1, 0, 0, 0, 0, 177, 142, 82, 61, 45, 23, 18, 7, 4,
2, 1, 0, 0, 182, 154, 49, 35, 28, 12, 7, 5, 2, 1, 0, 0, 0, 150,
132, 45, 26, 14, 10, 4, 5, 1, 1, 0, 0, 0, 188, 147, 84, 66, 34,
16, 8, 3, 1, 0, 0, 0, 0, 176, 152, 97, 65, 39, 31, 15, 4, 2,
2, 0, 0, 0, 153, 139, 78, 25, 20, 12, 5, 3, 1, 0, 0, 0, 0, 165,
140, 76, 34, 22, 18, 9, 2, 2, 0, 0, 0, 0, 175, 148, 98, 87, 35,
23, 10, 2, 1, 0, 0, 0, 0, 201, 169, 111, 91, 52, 36, 11, 8, 3,
1, 0, 0, 0, 183, 139, 88, 54, 38, 30, 8, 2, 2, 0, 0, 0, 0, 206,
175, 119, 68, 41, 34, 6, 3, 1, 1, 0, 0, 0), x = c(1, 2, 3, 4,
5, 6, 7, 8, 9, 10, 12, 14, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12,
14, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 12, 14, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 1,
2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 12, 14, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 1, 2, 3, 4,
5, 6, 7, 8, 9, 10, 12, 14, 1, 2, 3, 4, 5, 6, 7, 8, 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L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L), levels = c("A", "B", "C", "D"), class = "factor")), row.names = c(NA,
348L), class = "data.frame")

tl;dr I believe the results of these different approaches are equivalent (i.e. lead to the same distribution of results); they are not equal because the multivariate Normal values are chosen in a different way.
MASS::mvrnorm() computes the eigenvalues and eigenvectors of the covariance matrix; picks a set of standard Normal deviates; scales them by the square root of the eigenvalues; and multiplies by the eigenvector matrix
lme4::.simulateFun() (the workhorse function behind the simulate() method) computes the Cholesky factor of the covariance matrix of the random effects directly; picks a set of standard Normal deviates; and multiplies
For a simple case, #RolfTurner's roll-your-own code is much easier to understand; the advantage of the more abstract approach used in .simulateFun(), illustrated further below, is that it generalizes to an arbitrarily complex random effects model (multiple random effects, complicated vector-valued REs, etc.) without any further tweaking.
These two approaches give the same distributions, but not precisely the same answer (I hope that's convincing at a mathematical level: I don't really want to get farther into the details of exactly which linear combinations of standard normal deviates are used, or whether it's possible to permute the standard-normal-deviate vector so these two approaches are equivalent at the level of computed values, not just distributions ...)
Here is a simplified illustration of the two deviate-generation processes:
Sigma <- VarCorr(fit)[[1]]
set.seed(102)
b1 <- MASS::mvrnorm(1, mu = rep(0, 2), Sigma = Sigma)
## (Intercept) x
## -0.04550951 -0.01789231
set.seed(101)
b2 <- drop(t(chol(Sigma)) %*% rnorm(2))
## (Intercept) x
## -0.08147349 0.01498128
Here is (approximately) the code that lme4::.simulateFun uses internally (the code for MASS::mvrnorm() is much shorter and easier to read).
set.seed(101)
nsim <- 1
newRE <- mkNewReTrms(fit, newdata = Dat, ~x|batch)
## Lambdat is the (transpose of) the Cholesky factor of the RE covariance matrix
## Zt is the (transpose of) the RE model matrix
## (equivalent to converting the `Z[batch,]` vector to `Z[batch,1] + Z[batch,2]*x`)
U <- t(newRE$Lambdat %*% newRE$Zt)
u <- rnorm(ncol(U) * nsim)
b2 <- drop(as(U %*% matrix(u, ncol = nsim), "matrix"))
Here is an experimental demonstration (not proof) that the results are equivalent: simulate 500 draws using each approach and plot them against each other:
## NOT standalone: needs the code in the original post to be run first
ryo_fun <- function() {
b <- MASS::mvrnorm(nrep,c(0,0), Sigma)
lnpr <- with(Dat,beta0 + beta1*x + b[batch,1] + b[batch,2]*x)
p <- 1/(1+exp(-lnpr)) # Inverse of logit link.
size <- with(Dat,Good+Bad)
s.ryo <- rbinom(nrow(Dat),size,p)
return(s.ryo)
}
set.seed(101)
ryo_mat <- replicate(500, ryo_fun())
# Using simulate.merMod:
set.seed(101)
mer_mat <- sapply(simulate(fit, nsim = 500), function(x) x[,1])
png("SO73439211.png")
par(las=1, bty = "l")
plot(ryo_mat, mer_mat, col = adjustcolor("black", alpha.f = 0.1), pch = 16,
xlab = "roll your own", ylab = "simulate()")
abline(a=0, b=1, col = 2, lwd = 2)
dev.off()

Related

Using gratia::draw() in R to display partial effect plots within an HGAM that are not relative to the global smooth

I have a data set that looks like this:
df <- data.frame(
Lake = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 1L,
2L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L,
1L, 2L, 2L, 1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L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L,
2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L,
1L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L,
1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 1L,
1L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L,
1L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 2L,
1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 2L,
2L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 1L,
1L, 2L, 1L, 1L, 2L), .Label = c("Fork", "TBend"), class = "factor"),
Depth = c(1.63, 3, 10, 3, 10, 4, 13, 17, 14, 2.81, 20, 3, 28, 24, 6,
1.81999999999999, 7, 25, 2.20999999999998, 10, 15, 7.25999999999999,
4, 4, 6.64999999999998, 8.83999999999997, 6, 2.20999999999998,
22.96, 5.63, 11, 30, 32.31, 25, 1, 3, 4, 7.41000000000003, 2,
6, 17, 7, 5, 4.20999999999998, 3, 22, 5, 4.74000000000001, 7,
10, 3, 11, 14, 2, 24, 1, 7, 15, 16, 2.68000000000001, 12, 11,
5, 10, 10, 6, 12, 4, 4, 4.64999999999998, 18, 7.5, 13, 3, 15,
10, 22, 19, 4, 12, 2, 3, 5.41000000000003, 6, 19, 6, 3, 3, 34,
3.63, 11, 6, 7, 25, 4, 2.81, 4.70999999999998, 3, 12.31, 5, 17,
28, 3.63, 8, 9, 3, 30, 20, 11, 4, 12, 3, 4, 16, 5, 10, 2, 14,
58, 10, 2.06, 15, 2.74000000000001, 7, 10.74, 2.81, 11, 6, 5,
7.25999999999999, 10, 2.68000000000001, 9, 2.83999999999997,
5.5, 15, 7, 6.56, 14, 6, 3.25999999999999, 2.31, 1, 7, 3, 4,
2, 3, 9, 28, 18.84, 5, 5, 2.75999999999999, 7.63, 8.20999999999998,
18, 3, 11, 1, 24, 4, 22, 2, 3, 4.20999999999998, 14.65, 16, 9,
5, 3, 7, 1, 2, 4.5, 2, 20, 1, 10, 17, 4, 2, 1, 23, 5, 11, 12,
17, 10, 3, 18, 6, 7, 5, 3, 32, 16, 5, 7, 9, 29, 2, 12, 4, 23,
14, 4, 5, 11, 11.82, 6.20999999999998, 7, 12, 3, 6, 4, 17, 4,
24, 6, 12, 11.63, 4, 2, 25, 2, 54, 7, 8, 9.25999999999999, 14,
15, 11, 6, 21, 1, 3, 8, 1, 2.83999999999997, 19, 6, 19, 2.06,
3, 3, 4, 8, 6, 9.41000000000003, 4, 8.64999999999998, 3, 3, 2.5,
30, 12, 14, 15, 16, 10.56, 24, 12, 16.71, 25, 1, 10, 17, 1, 1.25999999999999,
12, 4, 24, 15, 8.68000000000001, 8, 3, 15.82, 17, 5, 3, 6.70999999999998,
5.63, 10, 10.68, 8, 3, 8.81, 5.25999999999999, 22, 12, 5.81999999999999,
6, 6, 3.5, 1.52999999999997, 4, 22, 15, 4, 23, 12, 25, 4, 22,
5.41000000000003, 9, 19, 8, 4, 8.56, 20, 10.21, 24, 1, 6, 3,
10, 3, 28, 12, 6, 17, 1, 3.41000000000003, 6.16000000000003,
4, 20.68, 4, 2.74000000000001, 5, 12, 1, 45, 4.74000000000001,
18, 15, 1, 8, 20, 21, 3, 16, 1, 3, 30, 10, 6.06, 4, 10.84, 25,
26, 12, 2.56, 2, 6, 10.56, 10.31, 16, 29.26, 5, 6, 3.81999999999999,
15, 1, 8, 3, 2, 22, 5, 2.95999999999998, 4.5, 1, 18, 2.66000000000003,
19, 12, 4, 14, 3, 7, 28, 4, 23, 6, 5, 3, 22, 1, 4, 12, 7, 1.63,
12.21, 15, 4, 3, 9, 20.65, 4.74000000000001, 22, 8.81, 5.81999999999999,
4.16000000000003, 7, 10, 24, 4.95999999999998, 30, 2, 10, 5,
9, 5, 12, 29.82, 2, 6.5, 6.20999999999998, 1, 1, 22, 22, 6.64999999999998,
32, 11, 15, 1, 18, 1.81999999999999, 4, 8, 20, 15, 4, 7, 22,
2, 2, 1, 1, 15, 20, 3, 5, 1.63, 4.66000000000003, 22, 6, 2, 31,
20, 5, 9.5, 30, 18, 13, 12, 12, 4.20999999999998, 12, 10.06,
2.68000000000001, 2, 1, 5, 2, 9, 2, 4, 1, 6, 1, 1, 2.16000000000003,
7, 8.95999999999998, 2.74000000000001, 5, 4, 5, 15, 20, 5.41000000000003,
29.41, 7, 32, 4, 14, 2.74000000000001, 4, 15, 8, 21, 32, 13.41,
3, 14, 4, 3, 18, 2.31, 25, 3.5, 4.74000000000001, 19, 21, 5.25999999999999,
10.21, 12.84, 2.95999999999998, 2, 4.31, 7, 7, 2.31, 17, 10.71,
23.41, 3, 3.41000000000003, 4.68000000000001, 22, 3, 13, 15,
8.74000000000001, 14.81, 5, 1, 4, 16, 1.41000000000003, 13, 3,
2, 6.06, 7, 3, 22, 4.83999999999997, 7, 2.81, 21, 3, 19, 6, 14,
2, 1, 10, 7.5, 8.70999999999998, 30, 14, 20, 1, 18, 30, 28, 1.41000000000003,
20, 5, 1.41000000000003, 3.5, 4.64999999999998, 5, 9.5, 3, 1.63,
11, 21, 2.66000000000003, 20.74, 15, 15, 14, 5, 14, 4.5, 4, 6.06,
4.20999999999998, 12, 18, 10.16, 7.81999999999999, 1, 2.95999999999998,
15, 2.5, 2.70999999999998, 11, 13.63, 18, 6, 18, 11, 6, 12, 7.5,
4.56, 1.38, 2.95999999999998, 17, 4, 1, 15, 4.74000000000001,
5.5, 11, 4, 1, 3, 25, 3, 9, 15, 11, 29, 8.56, 23, 14.65, 1, 7,
8, 14.06, 2, 3, 26, 2.56, 2.5, 25, 2.74000000000001, 1, 3, 8.56,
9.38, 2, 18, 3, 30, 16.96, 4, 22, 11, 6, 4, 3, 8.83999999999997,
22, 18, 7, 2.68000000000001, 6, 14.76, 7, 5, 1, 21, 3.81999999999999,
10, 3, 5, 7, 6, 20, 6.81, 7, 19, 24, 5, 1, 21.41, 3, 1.81999999999999,
10, 11.41, 6, 30, 3, 4, 4, 4, 1.5, 10.5, 18, 10, 2, 25, 14, 4,
5.63, 4.20999999999998, 2, 10.84, 10, 7, 30, 1, 17, 3, 3, 22,
2.74000000000001, 1, 8, 7, 32.65, 4, 3, 5, 4, 5, 1, 5, 10.76,
4, 2, 3.41000000000003, 4, 17),
OrdDay = c(254, 271, 286, 88, 181, 209, 246, 259, 218, 324, 230, 181,
271, 351, 364, 224, 268, 232, 210, 215, 260, 281, 286, 351, 195,
167, 248, 54, 308, 254, 322, 125, 33, 248, 336, 319, 322, 238,
181, 304, 195, 181, 273, 210, 153, 230, 28, 349, 195, 78, 286,
41, 355, 109, 78, 187, 31, 286, 41, 336, 187, 146, 305, 70, 290,
129, 290, 160, 83, 195, 147, 7, 159, 195, 146, 195, 181, 11,
349, 230, 140, 146, 268, 305, 181, 244, 299, 124, 155, 254, 232,
218, 12, 78, 286, 324, 177, 131, 33, 304, 56, 211, 254, 218,
60, 167, 147, 167, 322, 181, 299, 167, 215, 351, 230, 334, 25,
63, 11, 246, 5, 281, 349, 209, 91, 324, 246, 63, 203, 281, 167,
336, 63, 167, 88, 248, 153, 184, 237, 28, 281, 33, 195, 167,
109, 260, 56, 268, 248, 259, 187, 11, 124, 75, 254, 54, 218,
319, 322, 91, 12, 204, 195, 211, 125, 54, 195, 271, 364, 83,
335, 75, 75, 109, 75, 299, 160, 124, 334, 7, 146, 153, 184, 129,
146, 181, 131, 364, 31, 124, 11, 304, 290, 181, 204, 195, 322,
290, 305, 28, 336, 101, 174, 335, 109, 322, 273, 304, 364, 224,
210, 246, 25, 305, 349, 319, 83, 160, 28, 224, 187, 254, 124,
7, 167, 195, 12, 12, 187, 281, 101, 336, 304, 195, 244, 75, 232,
322, 246, 167, 237, 167, 336, 5, 125, 232, 187, 204, 286, 268,
131, 195, 322, 155, 104, 325, 28, 215, 195, 224, 184, 224, 174,
177, 167, 21, 363, 244, 268, 281, 286, 286, 335, 286, 336, 286,
109, 224, 181, 322, 299, 177, 254, 124, 336, 268, 218, 324, 281,
12, 119, 224, 248, 187, 215, 234, 159, 7, 204, 167, 78, 167,
325, 244, 290, 238, 305, 322, 246, 334, 184, 195, 210, 335, 160,
248, 218, 299, 78, 322, 167, 41, 211, 184, 238, 21, 281, 336,
322, 349, 268, 363, 273, 334, 349, 83, 78, 75, 204, 25, 237,
104, 232, 195, 319, 363, 355, 5, 335, 167, 237, 349, 286, 184,
75, 91, 184, 33, 215, 281, 28, 78, 224, 215, 116, 268, 124, 248,
7, 70, 308, 160, 336, 237, 105, 195, 273, 305, 273, 155, 248,
281, 160, 209, 259, 63, 101, 143, 67, 187, 203, 11, 254, 210,
31, 167, 363, 70, 195, 91, 41, 324, 224, 21, 351, 146, 268, 308,
28, 334, 259, 56, 12, 232, 174, 224, 101, 335, 54, 195, 143,
25, 171, 195, 167, 336, 281, 203, 25, 224, 75, 218, 248, 160,
181, 237, 195, 133, 172, 146, 75, 143, 260, 215, 56, 254, 105,
271, 319, 88, 364, 12, 230, 271, 125, 203, 248, 211, 286, 54,
63, 5, 336, 259, 105, 28, 299, 224, 172, 125, 75, 299, 177, 105,
21, 28, 308, 91, 88, 63, 281, 167, 349, 238, 238, 204, 12, 237,
349, 91, 364, 174, 237, 63, 363, 268, 167, 28, 181, 155, 160,
33, 304, 244, 349, 248, 28, 281, 54, 167, 308, 116, 33, 224,
181, 33, 364, 177, 268, 268, 238, 336, 281, 181, 299, 246, 349,
324, 56, 75, 273, 271, 268, 195, 246, 181, 5, 248, 146, 322,
167, 140, 324, 286, 286, 174, 322, 60, 187, 260, 335, 104, 177,
167, 203, 304, 177, 232, 336, 209, 238, 125, 260, 268, 203, 195,
363, 88, 232, 254, 203, 246, 105, 349, 268, 160, 336, 336, 260,
88, 56, 5, 54, 363, 31, 21, 224, 260, 308, 355, 25, 177, 167,
254, 224, 70, 349, 281, 119, 7, 75, 184, 124, 308, 273, 146,
202, 167, 349, 88, 218, 70, 210, 160, 147, 155, 181, 244, 195,
56, 184, 41, 195, 160, 260, 101, 5, 116, 230, 351, 184, 25, 224,
349, 91, 67, 184, 124, 355, 237, 167, 209, 308, 167, 268, 31,
218, 101, 155, 167, 12, 125, 143, 336, 286, 75, 167, 187, 260,
304, 224, 203, 290, 125, 195, 290, 355, 324, 153, 187, 349, 355,
324, 238, 260, 224, 281, 238, 140, 290, 273, 119, 181, 153, 129,
271, 75, 230, 116, 41, 91, 167, 254, 54, 290, 167, 11, 237, 336,
105, 181, 11, 286, 244, 349, 91, 230, 336, 195, 119, 230, 349,
349, 203, 238, 63, 75, 335, 91, 268, 322, 83),
stringsAsFactors = FALSE)
I am running an HGAM (GI model from Pederson et al. 2019 https://peerj.com/articles/6876/) that looks like this:
library(mgcv)
hgam_gi<-gam(Depth~Lake+s(OrdDay,bs="cc")+s(OrdDay,by=Lake,bs='cc')+s(Lake,bs="re"),data=df,family=nb)
My partial effect plots look like this:
library(gratia)
draw(hgam_gi)
My understanding of these partial effect plots is that the individual smooth LakeTBend doesn't differ much from the global smooth s(OrdDay) thus leadings to no effect in the plot and LakeFork has a stronger effect somewhere around OrdDay 200-250. I am wondering if there's a way to use an HGAM with a global smooth, and extract the individual smooths from that model similar to what you get with an HGAM without a global smooth.
With a model without a global smooth (I model according to Pederson et al. 2019), I can get a better understanding of the individual trends.
hgam_i<-gam(Depth~Lake+s(OrdDay,by=Lake,bs='cc'),data=df,family=nb)
draw(hgam_i)
My question is if there is a call I can make in gratia::draw() with an HGAM that can pull the partial effect plots independent of the global smooth? I know I can use predict.gam() to get the the individual predictions for each lake, which is great, but I really like the interpretability of partial effect plots and would love to find a way to run a single parsimonious model that displays global trends and individual trends.
Thanks!
Your models are somewhat mis-specified - you have the group (lake) means in the model twice:
the parametric Lake effect, and
the random effect s(Lake, bs = "re")
you shouldn't include this effect twice; use one or the other.
Also, your first model really should have something for the by = Lake smooths to help the model identify those lake-specific differences from the global smooth. Typically we would add m = 1 to those by = Lake smooths when the model already contains a "global" smooth. You should also set knots when fitting a bs = "cc" smooth of day (assuming OrdDay is day of year?):
library("mgcv")
library("dplyr")
knots <- list(OrdDay = c(0.5, 366.5))
m <- gam(Depth ~ Lake +
s(OrdDay, bs = "cc") +
s(OrdDay, by = Lake, bs = "cc", m = 1),
data = df, family = nb, knots = knots)
another way of specifying this model is using an ordered factor instead of a normal factor. We have to set the contrasts differently so that the parametric term is easier to understand
df <- df |>
mutate(oLake = ordered(Lake))
contrasts(df$oLake) <- contr.treatment
Now when you fit the model we use oLake in place of Lake and the first smooth of OrdDay represents the smooth for the reference level of oLake (here the reference is: Fork). The the by = oLake smooth (there is only one here as you only have two levels/lakes) represents the smooth difference between the two lakes, such that the smooth for TBend is the linear combination of the two smooths in the model
m2 <- gam(Depth ~ oLake +
s(OrdDay, bs = "cc") +
s(OrdDay, by = oLake, bs = "cc"),
data = df, family = nb, knots = knots)
With either model, to get estimated smooths for your two lakes, you need to add combinations of smooths. With m (the global smooth plus m = 1 smooths for each lake) we need to form the following combinations:
For Fork we want s(OrdDay) + s(OrdDay):LakeFork
For TBend we want s(OrdDay) + s(OrdDay):LakeTBend
For m2 where we used ordered factors, we want:
For Fork we want s(OrdDay)
For TBend we want s(OrdDay) + s(OrdDay):oLakeTBend
You can choose to include the group effects or not. Including them would mean you are actually generating fitted values on the response scale which is not what draw() shows (it shows partial effects).
To get fitted values we just predict over the range of OrdDay for both lakes. Using the development version of {gratia} on GitHub we create a data slice and then use fitted_values() on the data slice to yield predicted/fitted values:
ds <- data_slice(m, OrdDay = evenly(OrdDay, n = 100), Lake = evenly(Lake))
fv <- fitted_values(m, data = ds, scale = "response")
fv
library("ggplot2")
fv |>
ggplot(aes(x = OrdDay, y = fitted, group = Lake)) +
geom_ribbon(aes(ymin = lower, ymax = upper, fill = Lake), alpha = 0.2) +
geom_line(aes(colour = Lake))
while for m2 we use
ds2 <- data_slice(m2, OrdDay = evenly(OrdDay, n = 100), oLake = evenly(oLake))
fv2 <- fitted_values(m2, data = ds2, scale = "response")
fv2
library("ggplot2")
fv2 |>
ggplot(aes(x = OrdDay, y = fitted, group = oLake)) +
geom_ribbon(aes(ymin = lower, ymax = upper, fill = oLake), alpha = 0.2) +
geom_line(aes(colour = oLake)) +
labs(fill = "Lake", colour = "Lake") +
scale_colour_discrete() +
scale_fill_discrete()
If you don't want the group means, so you are seeing something similar to that draw() produces (partial effects), you'll need to currently switch back to predict.gam(), using type = "terms", and then sum the rows over the relevant columns:
p <- predict(m2, newdata = ds2, type = "terms")
p contains
r$> head(p)
oLake s(OrdDay) s(OrdDay):oLakeTBend
1 0.0000000 0.2274295 0.00000000
2 0.3846254 0.2274295 0.07137302
3 0.0000000 0.2286423 0.00000000
4 0.3846254 0.2286423 0.07287456
5 0.0000000 0.2263583 0.00000000
6 0.3846254 0.2263583 0.07405621
so we only want to sum the rows over columns 2 and 3:
fv3 <- rowSums(p[, -1])
Getting the standard errors is a bit more difficult in this case, so if including the group means is OK with you, I'll omit the code to get the correct standard errors for the type = "terms" idea. Comment if you need that.

putting segments on a plot with multiple plots and y variables

Is there a way to put arrows on a plot with multiple plots with different y axes? I would like to put arrows across the time series on the same x axis locations but different y locations. I cant just use "annotate("segment", x = 37, xend = 84, y = 0.0, yend = 0.0,colour = "black", size = 1, arrow = arrow(ends='both'))" because then it puts them at 0 on the y axis for all variables when I actually want to just put the arrows at the bottom of the y axis which is different for every variable.
Current code:
fin_plot <- ggplot(melted_data, aes(x = `Distance`, y = value, group = variable)) + geom_line() + theme_bw() + labs(y="", x= "") + theme_classic() + theme(text=element_text(size=16, family="serif", face = "bold", color = "black")) +
facet_wrap(variable~., scales = "free_y",ncol=2) +
scale_x_continuous(limits = c(0, 250),labels = scales::number_format(accuracy = 1)) + theme(axis.line = element_line(colour = 'black', size = 1)) +
theme(axis.ticks = element_line(colour = "black", size = 1)) + scale_y_continuous(labels = scales::number_format(accuracy = 0.1))+ theme(axis.ticks.length = unit(.3, "cm")) + coord_capped_cart(bottom='right', left='none', gap = 0.15) + geom_vline(xintercept=c(58, 132, 204, 250, 309), linetype='dashed', col = 'black')
Current output
desired output
data:
melted_data <- structure(list(Distance = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75,
76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105,
106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118,
119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131,
132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144,
145, 146, 147, 148, 149, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28,
29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76,
77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92,
93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106,
107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132,
133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145,
146, 147, 148, 149, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,
46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77,
78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93,
94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120,
121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133,
134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146,
147, 148, 149, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46,
47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62,
63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78,
79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94,
95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108,
109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134,
135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147,
148, 149, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108,
109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134,
135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147,
148, 149), variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L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, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L), .Label = c("Mg", "Mn", "Zn", "Ba", "All"), class = "factor"),
value = c(0.903247645, 0.912560748, 0.896003508, 0.909572697,
0.883631829, 0.905722594, 0.892465355, 0.909271173, 0.880506202,
0.889278401, 0.878534542, 0.959209459, 0.913303825, 0.929893977,
0.97778374, 0.9885554, 0.929716333, 1.028422583, 1.025638955,
1.011352651, 1.041343955, 1.092562951, 1.129761801, 1.088857171,
1.107257284, 1.116728405, 1.103053734, 1.041662037, 1.134182243,
1.104550315, 1.086952767, 1.106004784, 1.057688595, 1.034347579,
1.04641385, 1.139270945, 1.048446018, 1.033827731, 1.075554754,
1.029893202, 1.074749532, 1.001626205, 0.977053541, 0.987467665,
0.999540478, 0.945184816, 0.959677178, 0.962807712, 0.967023936,
1.024286493, 0.881264816, 0.967181342, 1.000316876, 0.956168258,
1.003214572, 1.00047837, 0.940103474, 0.929875987, 0.928227112,
0.982410241, 0.983035162, 0.976666772, 1.019755049, 1.075189042,
0.975380543, 0.981316782, 0.986876269, 1.026690916, 1.052379934,
1.001547298, 0.979888683, 1.008209647, 0.976098272, 0.944479556,
0.996767684, 1.018077758, 1.028862706, 1.08510417, 1.08963868,
1.048481179, 1.139954126, 1.107066353, 1.122920581, 1.23904326,
1.19449336, 1.179971969, 1.165865352, 1.068804094, 1.099436469,
1.073307737, 1.07045113, 1.101007051, 1.011962649, 1.11202545,
1.097883672, 1.05361424, 0.993283703, 1.046635444, 1.04951188,
1.055736151, 1.063705172, 0.977095039, 1.015650848, 1.029367222,
1.003814349, 0.973376993, 1.021665177, 0.925511352, 1.014703757,
0.933654542, 1.027336075, 0.961163947, 1.022921765, 0.910164297,
0.937410814, 0.935246588, 0.925900983, 0.934477753, 0.927973832,
0.946372309, 0.950554394, 0.9386026, 1.000712639, 0.947846812,
0.953585987, 0.967735737, 0.927914753, 0.943303715, 0.935435884,
0.987648375, 0.902379461, 0.939086878, 1.018529942, 0.973874968,
0.974093087, 0.984149676, 0.948669001, 0.934863295, 1.011232041,
0.942884239, 0.978044788, 1.023700208, 1.011714275, 0.999153709,
1.06822476, 0.967735328, 1.131133479, 1.011068503, 1.034903609,
0.086720869, 0.113119382, 0.088197332, 0.081547788, 0.079373211,
0.07888827, 0.072865285, 0.079637996, 0.066314774, 0.097585729,
0.185034982, 0.214466904, 0.294317625, 0.481389256, 0.531196058,
0.715842439, 0.865098887, 0.987242052, 1.081028291, 1.240920518,
1.313524957, 1.543771699, 1.78495042, 1.746572555, 2.048760527,
2.101438775, 1.967474033, 2.000286925, 2.014020838, 1.924470659,
1.75696549, 1.786681246, 1.633290961, 1.455799758, 1.315346538,
1.435348984, 1.27887702, 1.152818928, 1.095127218, 0.987502349,
1.062278922, 0.898540082, 0.83617998, 0.889057689, 0.825563648,
0.788347646, 0.790973555, 0.775541228, 0.815063004, 0.848723108,
0.66783059, 0.672629631, 0.747809615, 0.72338158, 0.666220438,
0.664051795, 0.597260657, 0.689282162, 0.663808452, 0.678551141,
0.672917354, 0.686199986, 0.724202364, 0.746195474, 0.686135659,
0.654148537, 0.713488795, 0.72446665, 0.699529989, 0.630120423,
0.661767463, 0.663290351, 0.705879842, 0.709399338, 0.76228353,
0.714368918, 0.720561695, 0.837036666, 0.923882149, 1.014163852,
1.221410703, 1.315825246, 1.368054705, 1.641746627, 1.630198312,
1.698589629, 1.562956393, 1.427322658, 1.53964983, 1.574583495,
1.527101216, 1.380123116, 1.28649445, 1.29251968, 1.330565441,
1.317758525, 1.19292313, 1.217953538, 1.218591815, 1.163372928,
1.091026791, 0.878691182, 0.903966928, 0.917620557, 0.838430901,
0.825709255, 0.839298558, 0.76309434, 0.97617394, 0.739885015,
0.822159341, 0.785335779, 0.771926988, 0.766619321, 0.832448556,
0.733734124, 0.787221188, 0.685452005, 0.740552711, 0.707414697,
0.781271754, 0.72652958, 0.729470139, 0.71649368, 0.681176551,
0.683977986, 0.711079301, 0.681092777, 0.747615639, 0.700953146,
0.692246657, 0.673560118, 0.820384633, 0.740567172, 0.72070082,
0.795192662, 0.773897168, 0.74552279, 0.735710787, 0.768825863,
0.746016457, 0.736542042, 0.744507532, 0.784312542, 0.758393534,
0.7600356, 0.797384742, 0.773626898, 0.744557896, 0.746612627,
0.818368055, 0.696689824, 0.748702805, 0.717457681, 0.766243608,
0.805305259, 0.855909762, 0.803357905, 0.889646097, 0.854456208,
1.067795473, 1.051422575, 1.17061972, 1.138440648, 1.052796919,
1.040998633, 1.161739158, 1.025956799, 0.971567748, 1.072911493,
0.952121155, 1.040392714, 1.069745522, 1.068549198, 1.090194087,
1.214584829, 1.157485471, 1.245813376, 1.336359991, 1.204038397,
1.126255292, 1.131057736, 0.922042386, 1.037566449, 1.100852394,
1.121842367, 0.998657748, 1.006938923, 1.002800377, 0.897387497,
0.93902937, 0.889327622, 0.802133735, 0.855245047, 0.860702407,
0.704324249, 0.905827093, 0.760155095, 0.760247698, 0.655991619,
0.677006743, 0.668001976, 0.623410532, 0.569302474, 0.523713794,
0.690042836, 0.539115342, 0.528696218, 0.57851915, 0.60294784,
0.581392042, 0.65277069, 0.65620614, 0.625397246, 0.697647782,
0.6180657, 0.632326126, 0.684659215, 0.606197513, 0.630134281,
0.637151517, 0.574538208, 0.605993607, 0.533522181, 0.544522236,
0.577535469, 0.573427383, 0.672984155, 0.735286828, 0.7532343,
0.881292245, 0.801132661, 1.122761046, 1.137397845, 1.173190388,
1.138033979, 1.126494557, 1.144871399, 1.087042815, 0.981750792,
0.992888445, 0.955352455, 1.074357698, 1.027127808, 1.083248059,
1.010304962, 1.037776316, 1.052809984, 0.959161909, 0.939369893,
0.932304641, 0.912110856, 1.035278327, 0.825391661, 0.883818816,
0.880397247, 0.775385156, 0.860535004, 0.75878312, 0.764243502,
0.788209749, 0.736029937, 0.746966542, 0.762295984, 0.804665042,
0.797845669, 0.744225613, 0.846139103, 0.806957411, 0.789078125,
0.912631032, 0.926629248, 0.807376002, 0.795165332, 0.776764645,
0.811532921, 0.740169463, 0.707007363, 0.764252403, 0.754265833,
0.656183602, 0.78602999, 0.734580057, 0.756587437, 0.750509131,
0.727536118, 0.676232276, 0.714439923, 0.720668076, 0.763533465,
0.60234143, 0.651920197, 0.744086872, 0.633919728, 0.615213712,
0.705944962, 0.667362984, 0.742636421, 0.742734852, 0.839492568,
0.743899849, 0.817080816, 0.773569657, 0.735728339, 0.715168283,
0.78077814, 0.694280484, 0.773303425, 0.768041196, 0.883401699,
0.818274274, 0.715927964, 0.696938222, 0.832246446, 0.73089346,
0.790965216, 0.799717389, 0.865896893, 0.946771069, 0.954212275,
1.023740345, 1.027036123, 1.086336263, 1.064542815, 0.9463809,
0.924081609, 0.999832641, 0.911277648, 0.922871168, 0.953134033,
0.786732115, 0.802026729, 0.832863371, 0.863952475, 0.817833153,
0.748586924, 0.72095701, 0.738213943, 0.672736744, 0.704947698,
0.531743532, 0.634123809, 0.683548549, 0.733277161, 0.608993729,
0.752162246, 0.568705823, 0.643172511, 0.597251486, 0.655514695,
0.583437677, 0.557676441, 0.646713866, 0.527005047, 0.578023512,
0.576281064, 0.600923204, 0.578475648, 0.551957027, 0.585007991,
0.623858699, 0.630936819, 0.636198589, 0.565476603, 0.658861425,
0.577557604, 0.629178306, 0.646092809, 0.566079299, 0.60953767,
0.680135261, 0.500802233, 0.704656678, 0.61109605, 0.645344144,
0.667139888, 0.734969576, 0.780062983, 0.783090234, 0.83005691,
0.905356723, 0.933746319, 0.947613375, 0.923115827, 0.873482691,
0.746883952, 0.850273618, 0.795256154, 0.800825928, 0.772630039,
0.749567395, 0.7823457, 0.772609842, 0.736269985, 0.699705666,
0.716860238, 0.65909369, 0.806743181, 0.604632102, 0.629103485,
0.669824708, 0.545219042, 0.605081484, 0.545598194, 0.612458887,
0.640840679, 0.568115521, 0.578270006, 0.642784637, 0.486235168,
0.608704086, 0.449107996, 0.603056279, 0.573624703, 0.527880861,
0.479058818, 0.608581986, 0.497792884, 0.736359035, 0.560758315,
0.59150912, 0.491623628, 0.646548159, 0.559243084, 0.554057512,
0.542344646, 0.583808567, 0.623315676, 0.521008383, 0.511710892,
0.633820855, 0.529775704, 0.590383598, 0.500021436, 0.602344336,
0.499887402, 0.534870849, 0.583225149, 0.623554367, 0.62596102,
0.585378422, 0.648988779, 0.577416685, 0.632021029, 0.644454559,
0.684966009, 0.595845502, 2.479315993, 2.683540753, 2.424790513,
2.556904106, 2.454032378, 2.486582811, 2.485804182, 2.625597071,
2.444459365, 2.649813652, 2.686066928, 3.124873535, 3.077318299,
3.297830917, 3.344358668, 3.589441204, 3.566707313, 3.968369009,
3.932341434, 4.08973781, 4.374551474, 4.54266808, 4.97884528,
4.932211371, 5.310903272, 5.372904082, 5.231493496, 5.123516042,
5.393849098, 5.276658613, 4.970827822, 4.972075355, 4.608769407,
4.214216452, 4.232190208, 4.539424798, 4.266998558, 3.933891331,
3.898577905, 3.758409871, 3.707152695, 3.544143355, 3.234304675,
3.312782898, 3.363897722, 3.32751203, 3.063968711, 3.396338279,
3.110947858, 3.27642981, 2.802338511, 2.972332411, 2.999566144,
2.860636811, 2.88545135, 2.715249006, 2.805430479, 2.734554555,
2.721654986, 2.81795618, 2.810857383, 2.829266791, 3.020586802,
3.108527475, 2.923112037, 2.898589704, 2.977292189, 2.961041296,
3.065747444, 2.883958043, 2.837869726, 2.918189185, 2.936651583,
2.760674734, 2.997230073, 2.888064962, 2.972304014, 3.162708107,
3.42147456, 3.577994842, 3.897689363, 4.134240754, 4.19746467,
4.937297252, 4.909702892, 4.974867813, 4.740338415, 4.369505261,
4.634231316, 4.530190201, 4.380129066, 4.246648651, 4.003376949,
4.261248528, 4.228186763, 4.190890809, 3.896217461, 4.019225536,
3.980007369, 3.985014169, 3.698733958, 3.417194347, 3.50155334,
3.527485148, 3.272718395, 3.228503258, 3.353819869, 3.104831527,
3.419528222, 3.010592683, 3.256523555, 3.020944643, 3.139582776,
2.872858156, 3.135211633, 3.047270457, 3.038848701, 2.843214189,
3.123247632, 2.958537301, 3.257263308, 3.138521527, 3.248321146,
2.963340122, 3.076476029, 2.987721452, 3.004584487, 2.906910601,
2.973867453, 3.0761696, 2.869900334, 2.78054149, 3.25876542,
2.978797901, 3.041764942, 3.029872905, 3.052446623, 2.856505763,
2.9962536, 3.015603327, 3.111149077, 2.9885447, 2.993520426,
3.176541902, 3.037954707, 2.975005669, 3.278917742, 3.137024394,
3.117943428)), row.names = c(NA, -745L), class = "data.frame")
Use geom_segment - this allows you to make use of the faceting variable. You will then want to pass a data frame with the respective x/xend/y/yend.
library(dplyr)
## create a data frame first for the segments
## it makes sense to use the mininimum of your y for each facet
annot_df <- melted_data %>%
group_by(variable) %>%
summarise(y = min(value), yend = min(value), x = 25, xend = 75)
ggplot(melted_data, aes(x = Distance, y = value, group = variable)) +
geom_line() +
## now use the new data frame for geom_segment
geom_segment(data = annot_df, aes(x = x, xend = xend, y = y, yend = yend),
arrow = arrow(ends = "both", length = unit(5, "pt"))) +
facet_wrap(variable~., scales = "free_y",ncol=2)
Created on 2022-07-14 by the reprex package (v2.0.1)

Variance-covariance matrix of repeated measurements from data in a long format?

I have repeated measurements data on 66 patients with either endogenous or exogenous depression (endo) and depression scores measured weekly for 0-5 weeks (hdrs, so six measurements per patients including baseline). The data is in a long format:
mydata <- structure(list(id = c(101, 101, 101, 101, 101, 101, 103, 103,
103, 103, 103, 103, 104, 104, 104, 104, 104, 104, 105, 105, 105,
105, 105, 105, 106, 106, 106, 106, 106, 106, 107, 107, 107, 107,
107, 107, 108, 108, 108, 108, 108, 108, 113, 113, 113, 113, 113,
113, 114, 114, 114, 114, 114, 114, 115, 115, 115, 115, 115, 115,
117, 117, 117, 117, 117, 117, 118, 118, 118, 118, 118, 118, 120,
120, 120, 120, 120, 120, 121, 121, 121, 121, 121, 121, 123, 123,
123, 123, 123, 123, 302, 302, 302, 302, 302, 302, 303, 303, 303,
303, 303, 303, 304, 304, 304, 304, 304, 304, 305, 305, 305, 305,
305, 305, 308, 308, 308, 308, 308, 308, 309, 309, 309, 309, 309,
309, 310, 310, 310, 310, 310, 310, 311, 311, 311, 311, 311, 311,
312, 312, 312, 312, 312, 312, 313, 313, 313, 313, 313, 313, 315,
315, 315, 315, 315, 315, 316, 316, 316, 316, 316, 316, 318, 318,
318, 318, 318, 318, 319, 319, 319, 319, 319, 319, 322, 322, 322,
322, 322, 322, 327, 327, 327, 327, 327, 327, 328, 328, 328, 328,
328, 328, 331, 331, 331, 331, 331, 331, 333, 333, 333, 333, 333,
333, 334, 334, 334, 334, 334, 334, 335, 335, 335, 335, 335, 335,
337, 337, 337, 337, 337, 337, 338, 338, 338, 338, 338, 338, 339,
339, 339, 339, 339, 339, 344, 344, 344, 344, 344, 344, 345, 345,
345, 345, 345, 345, 346, 346, 346, 346, 346, 346, 347, 347, 347,
347, 347, 347, 348, 348, 348, 348, 348, 348, 349, 349, 349, 349,
349, 349, 350, 350, 350, 350, 350, 350, 351, 351, 351, 351, 351,
351, 352, 352, 352, 352, 352, 352, 353, 353, 353, 353, 353, 353,
354, 354, 354, 354, 354, 354, 355, 355, 355, 355, 355, 355, 357,
357, 357, 357, 357, 357, 360, 360, 360, 360, 360, 360, 361, 361,
361, 361, 361, 361, 501, 501, 501, 501, 501, 501, 502, 502, 502,
502, 502, 502, 504, 504, 504, 504, 504, 504, 505, 505, 505, 505,
505, 505, 507, 507, 507, 507, 507, 507, 603, 603, 603, 603, 603,
603, 604, 604, 604, 604, 604, 604, 606, 606, 606, 606, 606, 606,
607, 607, 607, 607, 607, 607, 608, 608, 608, 608, 608, 608, 609,
609, 609, 609, 609, 609, 610, 610, 610, 610, 610, 610), week = structure(c(0,
1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3,
4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0,
1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3,
4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0,
1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3,
4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0,
1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3,
4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0,
1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3,
4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0,
1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3,
4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0,
1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4,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 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Exogenous",
"Endogenous"), class = "factor"), hdrs = c(26, 22, 18, 7,
4, 3, 33, 24, 15, 24, 15, 13, 29, 22, 18, 13, 19, 0, 22,
12, 16, 16, 13, 9, 21, 25, 23, 18, 20, NA, 21, 21, 16, 19,
NA, 6, 21, 22, 11, 9, 9, 7, 21, 23, 19, 23, 23, NA, NA, 17,
11, 13, 7, 7, NA, 16, 16, 16, 16, 11, 19, 16, 13, 12, 7,
6, NA, 26, 18, 18, 14, 11, 20, 19, 17, 18, 16, 17, 20, 22,
19, 19, 12, 14, 15, 15, 15, 13, 5, 5, 18, 22, 16, 8, 9, 12,
21, 21, 13, 14, 10, 5, 21, 27, 29, NA, 12, 24, 19, 17, 15,
11, 5, 1, 22, 21, 18, 17, 12, 11, 22, 22, 16, 19, 20, 11,
24, 19, 11, 7, 6, NA, 20, 16, 21, 17, NA, 15, 17, NA, 18,
17, 17, 6, 21, 19, 10, 11, 11, 8, 27, 21, 17, 13, 5, NA,
32, 26, 23, 26, 23, 24, 17, 18, 19, 21, 17, 11, 24, 18, 10,
14, 13, 12, 28, 21, 25, 32, 34, NA, 17, 18, 15, 8, 19, 17,
22, 24, 28, 26, 28, 29, 19, 21, 18, 16, 14, 10, 23, 20, 21,
20, 24, 14, 31, 25, NA, 7, 8, 11, 21, 21, 18, 15, 12, 10,
27, 22, 23, 21, 12, 13, 22, 20, 22, 23, 19, 18, 27, NA, 14,
12, 11, 12, NA, 21, 12, 13, 13, 18, 29, 27, 27, 22, 22, 23,
25, 24, 19, 23, 14, 21, 18, 15, 14, 10, 8, NA, 24, 21, 12,
13, 12, 5, 17, 19, 15, 12, 9, 13, 22, 25, 12, 16, 10, 16,
30, 27, 23, 20, 12, 11, 21, 19, 18, 15, 18, 19, 27, 21, 24,
22, 16, 11, 28, 27, 27, 26, 23, NA, 22, 26, 20, 13, 10, 7,
27, 22, 24, 25, 19, 19, 21, 28, 27, 29, 28, 33, 30, 22, 11,
8, 7, 19, 29, 30, 26, 22, 19, 24, 21, 22, 13, 11, 2, 1, 19,
17, 15, 16, 12, 12, 21, 11, 18, 0, 0, 4, 27, 26, 26, 25,
24, 19, 28, 22, 18, 20, 11, 13, 27, 27, 13, 5, 7, NA, 19,
33, 12, 12, 3, 1, 30, 39, 30, 27, 20, 4, 24, 19, 14, 12,
3, 4, NA, 25, 22, 14, 15, 2, 34, NA, 33, 23, NA, 11)), row.names = c(NA,
-396L), class = "data.frame")
And looks like this:
head(reisby_long)
id week week_fact endo hdrs
1 101 0 Week 0 Exogenous 26
2 101 1 Week 1 Exogenous 22
3 101 2 Week 2 Exogenous 18
4 101 3 Week 3 Exogenous 7
5 101 4 Week 4 Exogenous 4
6 101 5 Week 5 Exogenous 3
My question is if I can obtain a variance-covariance matrix from this dataset without converting it to a wide format first. I ask because I have another dataset for which converting to a wide format is going to take me a long time (because I'm not very experienced in doing so) and the only reason for doing it would be to get a variance-covariance matrix.
Thanks!
Not sure if you can use that directly, but you wouldn't have to manually change the data to a wide format. You can use acast from the reshape2 library.
library(reshape2)
mat <- reshape2::acast(data = mydata, formula = id ~ week)
You can change the formula to obtain whichever rows and columns data is needed.

R get linear regression equation for boxplots

I didn´t found a sufficient answer in this forum yet, so I decided to raise my own question.
I want to get the linear regression equation of a linear fit from a boxplot. I have this data:
library(ggplot2)
data <- structure(list(x = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L
), .Label = c("1", "2", "3", "4", "5", "6"), class = "factor"),
y = c(169, 79.5, 78.5, 75, 99.5, 68, 14, 30.5, 107.5, 51,
43, 33, 21.5, 35, 11, 1, 38, 54.5, 26.5, 143, 158, 171, 31.5,
67.5, 1, 57.5, 12, 36.5, 1, 23.5, 22.5, 71, 141, 218, 7.5,
1, 129, 144.5, 76, 46.5, 75.5, 45, 12, 24, 67, 65.5, 44.5,
37.5, 25.5, 19, 15, 1, 17.5, 50, 22.5, 90, 226, 220, 32,
69.5, 1, 79.5, 7, 44, 1, 15.5, 22, 75.5, 178, 153, 4.5, 1,
159, 89, 57, 71, 98.5, 47.5, 18.5, 30, 119, 57.5, 41, 33.5,
30, 31, 10, 1, 12, 43.5, 20.5, 98, 146.5, 145, 34, 64.5,
1, 40.5, 17, 41, 1, 14.5, 16.5, 71, 181, 168, 2, 1, 159,
103, 69, 65.5, 97.5, 37.5, 21, 15.5, 120.5, 46, 27, 29.5,
16.5, 20, 7.5, 1, 15.5, 42.5, 21.5, 111, 102.5, 124, 20.5,
51.5, 1, 22.5, 15, 42, 1, 13, 13.5, 64.5, 138, 155, 4.9,
1, 190, 89.5, 74.5, 79, 78, 59.5, 19.5, 21, 88.5, 44, 18,
19, 10, 13, 4, 1, 9.5, 44, 17, 140.5, 98, 112.5, 29.5, 62.56,
1, 31, 11.5, 49.5, 1, 10, 8.5, 40.5, 121, 141, 2.5, 1, 170,
87.5, 92, 77, 65, 34, 8, 26, 98, 51.5, 26, 19, 9, 8.5, 7.5,
1, 4.5, 0, 15.5, 80, 69, 59, 28, 44.5, 1, 38.5, 10, 51.5,
1, 3, 5, 65, 107, 152, 5, 1)), row.names = c(NA, -216L), class = "data.frame")
p <- ggplot(data = data) +
aes(x = x,
y = y) +
geom_boxplot(outlier.shape = NA) + geom_jitter(shape = 1, position = position_jitter(0.1)) +
ylim(0, NA) +
theme_light() +
geom_smooth(method = "lm",se = TRUE, formula = y ~ x, aes(group = 1))
print(p)
fit <- lm(y ~ x, data = data)
fit
which results in this output:
How can I extract the regression equation for this dataset? The function fit <- lm(y ~ x, data = data) just gives me one intercept and 5 coefficients, which is not my desired output. I want a simple regression equation in the form of y = a + bx.
How can I put this equation into the diagramm? I´ve already looked into ggpmisc::stat_poly_eq(), but this doesn´t seem to work with boxplot linear regression.
Can you guys help me out?

ggplot2 - Survival Swimmer Plot Not Displaying Sorted Values

Using this question I've been assembling a customized Swimmer Plot, but despite my having sorted the patients by Therapy.Length, when I try to plot it the second half of the bars often come out much longer than they should be - putting the graph out of order.
I've checked and the symbols are in the correct place even when the bar is too long, so somehow the bar lengths are getting extended. By commenting out scale_y_continuous chunk, I'm able to get the proper length on the bars, but then all of the symbol placement is compressed.
ggplot(dat,
aes(Patient.ID, Therapy.Length)) +
geom_bar(stat="identity", aes(fill=factor(Disease.Stage)), width=0.7) +
geom_point(data=dat, aes(Patient.ID, value, colour=variable, shape=variable), size=4) +
geom_segment(data=dat %>% filter(Continued == 1),
aes(x=Patient.ID, xend=Patient.ID, y=Therapy.Length + 0.1, yend=Therapy.Length + 20),
arrow=arrow(type="closed", length=unit(0.13,"in"))) +
coord_flip() +
scale_fill_manual(values=hcl(seq(15,375,length.out=5)[1:4],100,70)) +
scale_colour_manual(values=c(hcl(seq(15,375,length.out=3)[1:2],100,40),c("black","darkgreen"))) +
scale_y_continuous(limits=c(-1,max(dat$Therapy.Length)), breaks=seq(0,max(dat$Therapy.Length),30)) +
labs(fill="Disease Stage", colour="", shape="", x="Subject Received Study Drug")
+ theme_bw() +
theme(panel.grid.minor=element_blank(),panel.grid.major=element_blank(),
axis.text.y=element_blank(),axis.ticks.y=element_blank())
Here's the rest of the code: [x]
ANSWER:
dat had originally been melted – changed dat to be unmelted (and created dat.m to be the melted version), and supplied dat.mto geom_point and geom_segment for adding points to bars.
data:
structure(list(Patient.ID = structure(c(1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 1L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 1L, 2L, 3L,
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L), .Label = c("13",
"5", "14", "11", "6", "2", "7", "12", "3", "8", "1", "10", "4",
"15", "9"), class = "factor"), Disease.Stage = c(1L, 2L, 4L,
1L, 2L, 3L, 1L, 1L, 3L, 2L, 1L, 1L, 3L, 2L, 3L, 1L, 2L, 4L, 1L,
2L, 3L, 1L, 1L, 3L, 2L, 1L, 1L, 3L, 2L, 3L, 1L, 2L, 4L, 1L, 2L,
3L, 1L, 1L, 3L, 2L, 1L, 1L, 3L, 2L, 3L, 1L, 2L, 4L, 1L, 2L, 3L,
1L, 1L, 3L, 2L, 1L, 1L, 3L, 2L, 3L), Response.Start = c(15, 40,
51, 26, 36, 26, 32, 43, 67, 80, 93, 105, 160, 34, 94, 15, 40,
51, 26, 36, 26, 32, 43, 67, 80, 93, 105, 160, 34, 94, 15, 40,
51, 26, 36, 26, 32, 43, 67, 80, 93, 105, 160, 34, 94, 15, 40,
51, 26, 36, 26, 32, 43, 67, 80, 93, 105, 160, 34, 94), Therapy.Length = c(74,
84, 84, 108, 128, 185, 194, 198, 257, 274, 293, 311, 325, 336,
357, 74, 84, 84, 108, 128, 185, 194, 198, 257, 274, 293, 311,
325, 336, 357, 74, 84, 84, 108, 128, 185, 194, 198, 257, 274,
293, 311, 325, 336, 357, 74, 84, 84, 108, 128, 185, 194, 198,
257, 274, 293, 311, 325, 336, 357), Continued = c(1, 1, 1, NA,
1, NA, 1, NA, 1, NA, 1, NA, 1, NA, 1, 1, 1, 1, NA, 1, NA, 1,
NA, 1, NA, 1, NA, 1, NA, 1, 1, 1, 1, NA, 1, NA, 1, NA, 1, NA,
1, NA, 1, NA, 1, 1, 1, 1, NA, 1, NA, 1, NA, 1, NA, 1, NA, 1,
NA, 1), variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L), .Label = c("Response.End", "Durable", "Complete",
"Partial"), class = "factor"), value = c(40, 78, 78, 106, 89,
177, 108, 101, 138, 209, 279, 244, 311, 167, 271, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, -1, NA, NA, NA, NA, NA, 40, NA, NA,
NA, NA, 32, 43, NA, NA, 93, NA, 160, NA, 94, 15, NA, 51, 26,
36, 26, NA, NA, 67, 80, NA, 105, NA, 34, NA)), row.names = c(NA,
-60L), .Names = c("Patient.ID", "Disease.Stage", "Response.Start",
"Therapy.Length", "Continued", "variable", "value"), class = "data.frame")

Resources