Reputation: 378
As part of a Shiny app I am developing, I am creating an "events analysis" tool, of which the basis of is using interrupted time series regression. Here is a reproducible example of interrupted time series analysis:-
library(ggplot2)
library(dplyr)
int = 22
set.seed(42)
df <- data.frame(
count = as.integer(rpois(132, 9) + rnorm(132, 1, 1)),
time = 1:132,
at_risk = rep(
c(4305, 4251, 4478, 4535, 4758, 4843, 4893, 4673, 4522, 4454, 4351),
each = 12
)
)
View(df)
df$month <- factor(month.name, levels = month.name)
df$intv <- ifelse(df$time >= int, 1, 0)
df$intv_trend <- c(rep(0, (int - 1)),
1:(length(unique(df$time)) - (int - 1)))
df <- df %>%
mutate(lag_count = dplyr::lag(count))
fit <- glm(count ~ month + time + intv + intv_trend +
log(lag_count) + offset(log(at_risk)),
family = "poisson", data = df)
summary(fit)
df$fit <- exp(c(NA, predict(fit)))
# Get predictions on the same scale as the data
df$fit2 = c(NA, predict(fit, type="response"))
# Add a grouping variable manually
df$group = rep(c("Control","Intervention"), c(72, 132 - 72))
ggplot(df, aes(x=time, y = count)) + geom_line()
ggplot(df, aes(x = time, y = fit2)) +
geom_line() +
geom_smooth(method="lm", se=F, aes(colour=group)) +
theme_bw() +
labs(colour="")
ggplot(df, aes(x = time, y = fit2)) +
geom_line() +
geom_smooth(method="loess", se=T, aes(colour=group)) +
theme_bw() +
labs(colour="")
ggplot(df, aes(x = time, y = fit2)) +
geom_point() +
geom_smooth(method="loess", se=F, aes(colour=group)) +
theme_bw() +
labs(colour="")
What I wanted to do was to return a statement telling the user whether a particular predictor variable was statistically significant or not; in this case, the predictor variable in question is "intv". In this model, intv
was not significant, so I would like a command that would return a statement, something like the following:-
#if result is significant:-
"The intervention/event had a significant impact on the dependent variable"
#or if it was non-significant:-
"The intervention/event had a non-significant impact on the dependent variable"
Or to that effect at least. Any help is appreciated!
Upvotes: 0
Views: 393
Reputation: 68
You can try this:
# Statements
significant <- "The intervention/event had a significant impact on the dependent variable"
non_significant <- "The intervention/event had a non-significant impact on the dependent variable"
# Create a data frame with the model's coefficients and p-values
coef_p <- data.frame(p_value = summary(fit)[["coefficients"]][,"Pr(>|z|)"]) %>%
tibble::rownames_to_column("coef") %>%
dplyr::mutate(statement = dplyr::if_else(p_value < 0.05, significant, non_significant, "NA"))
And if I understand correctly, the user must indicate an input (predictor), then you can create an object to store that input and then filter the result (p-value) and print corresponding statement, like this:
predictor = "intv"
result = filter(coef_p, coef == predictor) %>% pull(statement) %>% print(.)
Upvotes: 1