Specification curve analysis
# Grilling the data: Application of specification curve analysis to red meat and all-cause mortality 24: r
install.packages("specr")
install.packages("tidyverse")
library(tidyverse)
library(specr)
# Function to create logistic regression models
log_glm <- function(formula, data) {
glm(formula, data = data, family = binomial())
}
# Simulate example data
set.seed(123) # For reproducibility
n <- 1000 # Number of observations
example_data <- tibble(
x1 = rnorm(n, mean = 50, sd = 10), # Simulate red meat consumption
c1 = sample(c("Male", "Female"), n, replace = TRUE), # Sex
c2 = rnorm(n, mean = 50, sd = 10), # Age
c3 = rnorm(n, mean = 25, sd = 5), # BMI
c4 = rbinom(n, 1, 0.2), # Diabetes status (0 = no, 1 = yes)
y1 = as.factor(rbinom(n, 1, plogis(-3 + 0.02 * x1 + 0.1 * c4))) # Mortality (logistic model)
)
# Setup the specifications for the logistic regression model
specs <- setup(data = example_data,
x = "x1",
y = "y1",
model = "log_glm",
controls = c("c1", "c2", "c3", "c4"))
# Print the summary of the setup
summary(specs)
# Run the model using specr
results <- specr(specs)
# Show the first few rows of the results
results$data
plot(results)
留言
張貼留言