setwd('C:/Users/BASH0002/Desktop/predictionAnalysis')
library(readxl)
MDI <- read_excel("C:/Users/BASH0002/Desktop/predictionAnalysis/MDI.xlsx")
# Load the reshape2 package
library(reshape2)
MDI$ID <- seq(1:176)
# Reshape data from wide to long format
DT.m1 = melt(MDI, measure.vars = c('bmi1','bmi2','bmi4','bmi6','bmi8'), variable.name = "week", value.name = "BMI")
#colnames(DT.m1)
longDT <- DT.m1
longDT <- longDT[, c(7,8,9,1,4,5,2,3,6)]
#head(longDT)
# Create an empty list to store subsets for each ID
subset_list <- list()
# Iterate over each ID
for (i in 1:176) {
# Subset data for each ID and specified weeks
subset_data <- longDT[(longDT$ID == i & longDT$week %in% c('bmi1', 'bmi2', 'bmi4', 'bmi6', 'bmi8')), ]
# Append the subset to the list
subset_list[[i]] <- subset_data
}
# Combine all subsets into a single dataframe
final_subset <- do.call(rbind, subset_list)
# Print the final subset
head(final_subset)
## ID week BMI MDIscore Alder Varighedafspiseforstyrrelse
## 1 1 bmi1 15.37 35 51 30
## 177 1 bmi2 15.27 35 51 30
## 353 1 bmi4 15.64 35 51 30
## 529 1 bmi6 16.00 35 51 30
## 705 1 bmi8 15.77 35 51 30
## 2 2 bmi1 19.11 38 20 2
## PrimærdiagnoseICD10 Anoreksiundertype
## 1 F50.0 Anorexia Nervosa, typical F50.01 Restricting type
## 177 F50.0 Anorexia Nervosa, typical F50.01 Restricting type
## 353 F50.0 Anorexia Nervosa, typical F50.01 Restricting type
## 529 F50.0 Anorexia Nervosa, typical F50.01 Restricting type
## 705 F50.0 Anorexia Nervosa, typical F50.01 Restricting type
## 2 F50.1 Anorexia Nervosa, atypical F50.01 Restricting type
## Anoreksinuværendesværhedsgrad
## 1 Ekstrem
## 177 Ekstrem
## 353 Ekstrem
## 529 Ekstrem
## 705 Ekstrem
## 2 Moderat
longmdi <- final_subset
#save(longmdi, file = 'longmdi.RData')
Here, a linear mixed-effects model (LMEM) is a suitable analysis for given scenario. LMEMs account for the repeated measures within each individual (BMI scores over weeks) and allow to examine the effect of depression on BMI change while considering the covariates (age, diagnosis, etc.).
# Load required libraries
library(lme4)
## Indlæser krævet pakke: Matrix
data <- longmdi
# Assuming your data is in a dataframe called 'data'
# with columns: '(MDIscore)depression' (continuous), 'bmi' (continuous),
# 'age' (numeric), 'diagnosis' (categorical), 'anorexia_type' (categorical),
# 'duration' (numeric), and 'severity' (numeric) etc.
#summary(data)
# Define the LME model formula
lme_model <- lmer(BMI ~ MDIscore * week + Alder + Varighedafspiseforstyrrelse + Anoreksiundertype + PrimærdiagnoseICD10+
Anoreksinuværendesværhedsgrad + (1|ID), data = data)
# Explanation of the formula:
# - BMI is the dependent variable (predicted)
# - (MDIscore)depression and week are fixed effects (predictors)
# - The interaction term (depression * week) tests if the effect of depression
# on BMI changes over time (weeks).
# - age, diagnosis, anorexia_type, duration, and severity are covariates.
# - (1|ID) specifies a random intercept for each participant (ID) to account
# for individual differences in baseline BMI.
summary(lme_model)
## Linear mixed model fit by REML ['lmerMod']
## Formula: BMI ~ MDIscore * week + Alder + Varighedafspiseforstyrrelse +
## Anoreksiundertype + PrimærdiagnoseICD10 + Anoreksinuværendesværhedsgrad +
## (1 | ID)
## Data: data
##
## REML criterion at convergence: 2998.9
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -4.1724 -0.2687 -0.0160 0.2688 20.3765
##
## Random effects:
## Groups Name Variance Std.Dev.
## ID (Intercept) 1.628 1.276
## Residual 1.718 1.311
## Number of obs: 789, groups: ID, 176
##
## Fixed effects:
## Estimate Std. Error
## (Intercept) 15.494478 0.666136
## MDIscore 0.017813 0.015056
## weekbmi2 0.071312 0.508775
## weekbmi4 0.701440 0.520355
## weekbmi6 1.265467 0.533428
## weekbmi8 1.793231 0.561878
## Alder -0.013136 0.016604
## Varighedafspiseforstyrrelse 0.005271 0.020339
## AnoreksiundertypeF50.02 Binge-eating/purging type -0.019653 0.252039
## PrimærdiagnoseICD10F50.1 Anorexia Nervosa, atypical 0.946852 0.249627
## AnoreksinuværendesværhedsgradEkstrem -1.619626 0.287563
## AnoreksinuværendesværhedsgradMild 2.280814 0.347890
## AnoreksinuværendesværhedsgradModerat 0.961394 0.296462
## MDIscore:weekbmi2 -0.001027 0.015206
## MDIscore:weekbmi4 -0.001556 0.015563
## MDIscore:weekbmi6 -0.005289 0.015954
## MDIscore:weekbmi8 -0.009489 0.016675
## t value
## (Intercept) 23.260
## MDIscore 1.183
## weekbmi2 0.140
## weekbmi4 1.348
## weekbmi6 2.372
## weekbmi8 3.191
## Alder -0.791
## Varighedafspiseforstyrrelse 0.259
## AnoreksiundertypeF50.02 Binge-eating/purging type -0.078
## PrimærdiagnoseICD10F50.1 Anorexia Nervosa, atypical 3.793
## AnoreksinuværendesværhedsgradEkstrem -5.632
## AnoreksinuværendesværhedsgradMild 6.556
## AnoreksinuværendesværhedsgradModerat 3.243
## MDIscore:weekbmi2 -0.068
## MDIscore:weekbmi4 -0.100
## MDIscore:weekbmi6 -0.332
## MDIscore:weekbmi8 -0.569
##
## Correlation matrix not shown by default, as p = 17 > 12.
## Use print(x, correlation=TRUE) or
## vcov(x) if you need it
#print(model, correlation=TRUE)
#plot(lme_model)
# Extract model residuals
#model_residuals <- residuals(lme_model)
# qqnorm plot to assess normality
#qqnorm(model_residuals)
#qqline(model_residuals)
# Plot residuals vs fitted values
#plot(fitted(lme_model), model_residuals)
The summary output provides details on fixed and random effects:
(MDIscore)depression: Look at the p-value. A significant p-value (e.g., < 0.05) suggests depression predicts changes in BMI.
The coefficient indicates the direction and magnitude of the effect on average across weeks. Positive: Higher depression scores associated with larger BMI changes (increase or decrease depending on data).
week: This coefficient shows the average change in BMI per week (ignoring depression).
depression:week (interaction): The p-value for this term is crucial. A significant p-value suggests the effect of depression on BMI changes over time (weeks).
Covariates: Interpret coefficients and p-values of age, diagnosis, etc., to understand their influence on BMI.
(1|ID): This variance component shows inter-individual variability in BMI changes.
Main Effect of Depression:
If the depression term is significant, it suggests that, on average across all weeks, individuals with higher depression scores have different BMI trajectories compared to those with lower scores.
Effect of Week: The week term signifies the average change in BMI per week for all participants, regardless of depression level.
Interaction Effect (depression:week): A significant interaction term indicates that the effect of depression on BMI changes over time. You’ll need further investigation (post-hoc tests) to understand this interaction.
Covariates: Analyze the coefficients and p-values of covariates to see if age, diagnosis, etc., influence BMI changes independently of depression.