Skip to content

Commit 9200bd8

Browse files
authored
Merge pull request #162 from danforthcenter/frem-interactions
consistent interactions in frem
2 parents ce2d020 + 4d195d6 commit 9200bd8

2 files changed

Lines changed: 38 additions & 53 deletions

File tree

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# pcvr 1.3.0.4
2+
3+
Simplified logic in `frem` to always use an interaction term between design variables.
4+
15
# pcvr 1.3.0.3
26

37
Adding option to return exact parameters per each subject simulated in `growthSim`.

R/frem.R

Lines changed: 34 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,11 @@ frem <- function(df, des, phenotypes, timeCol = NULL, cor = TRUE, returnData = F
9090
df <- formatted$data
9191
timeCol <- formatted$timeCol
9292
#* `Make formulas`
93-
ext <- FALSE
94-
if (length(des) == 2) {
95-
ind_fmla <- paste0("(1|", des[1], ")+(1|", des[2], ")+(1|", des[1], ":", des[2], ")")
96-
} else {
97-
ind_fmla <- paste(paste0("(1|", des, ")"), collapse = "+")
98-
ext <- TRUE
99-
}
93+
int <- paste(des, collapse = ":")
94+
if (!grepl(":", int)) {
95+
int <- NULL
96+
} # no interaction for 1L des
97+
ind_fmla <- paste(paste0("(1|", c(des, int), ")"), collapse = "+")
10098
#* `Find time and subset data`
10199
if (is.null(time)) {
102100
dat <- na.omit(df[df[[timeCol]] == max(df[[timeCol]]), c(des, phenotypes, timeCol)])
@@ -111,33 +109,17 @@ frem <- function(df, des, phenotypes, timeCol = NULL, cor = TRUE, returnData = F
111109
LONGITUDINAL <- TRUE
112110
dat <- na.omit(df[, c(des, phenotypes, timeCol)])
113111
}
114-
115112
#* `Partition Variance`
113+
H2 <- .partitionVarianceFrem(dat, timeCol, phenotypes, ind_fmla, des, ...)
116114

117-
H2 <- .partitionVarianceFrem(dat, timeCol, phenotypes, ind_fmla, ext, des, ...)
118-
119-
if (!ext) {
120-
colnames(H2) <- c(des[1], des[2], "Interaction", "Unexplained", timeCol, "singular", "Phenotypes")
121-
} else {
122-
colnames(H2) <- c(des, "Unexplained", timeCol, "singular", "Phenotypes")
123-
}
124115
ordering <- H2[H2[[timeCol]] == max(H2[[timeCol]]), ]
125116
H2$Phenotypes <- ordered(H2$Phenotypes, levels = ordering$Phenotypes[order(ordering$Unexplained)])
126117
h2_melt <- data.frame(data.table::melt(as.data.table(H2), id = c("Phenotypes", "singular", timeCol)))
127-
128-
if (!ext) {
129-
h2_melt$variable <- ordered(h2_melt$variable,
130-
levels = c("Unexplained", des[1], des[2], "Interaction")
131-
)
132-
} else {
133-
h2_melt$variable <- ordered(h2_melt$variable,
134-
levels = c("Unexplained", des)
135-
)
136-
}
118+
h2_melt$variable <- ordered(h2_melt$variable,
119+
levels = c("Unexplained", des, "Interaction")
120+
)
137121
anova_dat <- h2_melt
138-
139122
#* `Plot Variance`
140-
141123
plotHelperOutputs <- .fremPlotHelper(
142124
LONGITUDINAL, anova_dat, markSingular, dummyX, timeCol, dat, phenotypes,
143125
cor, combine
@@ -277,7 +259,7 @@ frem <- function(df, des, phenotypes, timeCol = NULL, cor = TRUE, returnData = F
277259
#' @keywords internal
278260
#' @noRd
279261

280-
.partitionVarianceFrem <- function(dat, timeCol, phenotypes, ind_fmla, ext, des, ...) {
262+
.partitionVarianceFrem <- function(dat, timeCol, phenotypes, ind_fmla, des, ...) {
281263
H2 <- data.frame(do.call(rbind, lapply(sort(unique(dat[[timeCol]])), function(tm) {
282264
sub <- dat[dat[[timeCol]] == tm, ]
283265
des_bools <- unlist(lapply(des, function(var) {
@@ -298,37 +280,36 @@ frem <- function(df, des, phenotypes, timeCol = NULL, cor = TRUE, returnData = F
298280
}
299281
re <- lme4::VarCorr(model)
300282
res <- attr(lme4::VarCorr(model), "sc")^2
301-
302-
if (!ext) {
303-
interaction.var <- as.numeric(attr(re[[which(grepl(":", names(re)))]], "stddev"))^2
304-
des1.var <- as.numeric(attr(re[[des[1]]], "stddev"))^2
305-
des2.var <- as.numeric(attr(re[[des[2]]], "stddev"))^2
306-
307-
tot.var <- sum(as.numeric(re), res)
308-
unexp <- 1 - sum(as.numeric(re)) / sum(as.numeric(re), res)
309-
310-
h2 <- c(
311-
(des1.var / tot.var),
312-
(des2.var / tot.var),
313-
(interaction.var / tot.var),
314-
unexp,
315-
tm,
316-
singular
283+
des.var <- unlist(
284+
lapply(
285+
des,
286+
function(ds) {
287+
return(as.numeric(attr(re[[ds]], "stddev"))^2)
288+
}
317289
)
318-
} else {
319-
var <- lapply(des, function(i) {
320-
return(as.numeric(attr(re[[i]], "stddev"))^2)
321-
})
322-
323-
tot.var <- sum(as.numeric(re), res)
324-
unexp <- 1 - sum(as.numeric(re)) / sum(as.numeric(re), res)
325-
326-
h2 <- c(unlist(var) / tot.var, unexp, tm, singular)
290+
)
291+
if (length(des) > 1) {
292+
interaction.var <- as.numeric(attr(re[[which(grepl(":", names(re)))]], "stddev"))^2
293+
des.var <- c(des.var, interaction.var)
327294
}
295+
tot.var <- sum(as.numeric(re), res)
296+
unexp <- 1 - (sum(as.numeric(re)) / sum(as.numeric(re), res))
297+
298+
h2 <- c(
299+
(des.var / tot.var),
300+
unexp,
301+
tm,
302+
singular
303+
)
328304
return(h2)
329305
}))
330306
return(pheno_df)
331307
})))
332308
H2$Phenotypes <- rep(phenotypes, length.out = nrow(H2))
309+
des_colnames <- des
310+
if (length(des) > 1) {
311+
des_colnames <- c(des, "Interaction")
312+
}
313+
colnames(H2) <- c(des_colnames, "Unexplained", timeCol, "singular", "Phenotypes")
333314
return(H2)
334315
}

0 commit comments

Comments
 (0)