forked from acohenstat/STA6257_Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
temp_17mar2024.qmd
108 lines (77 loc) · 2.2 KB
/
temp_17mar2024.qmd
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
---
title: "R Model"
format: html
editor: visual
---
```{r}
library(nlme)
library(lme4)
library(readr)
library(Matrix)
library(nlme)
library(car)
library(ggplot2)
BMI_IOS_SCD_Asthma <- read_csv("BMI_IOS_SCD_Asthma.csv")
```
```{r}
bmi_data=BMI_IOS_SCD_Asthma
```
```{r}
head(bmi_data)
```
```{r}
colnames(bmi_data) <- c("Group", "Subject_ID", "Observation_number", "Hydroxyurea", "Asthma", "ICS", "LABA", "Gender", "Age_months", "Height_cm", "Weight_Kg", "BMI", "R5Hz_PP", "R20Hz_PP", "X5Hz_PP", "Fres_PP")
```
```{r}
bmi_data_clean = na.omit(bmi_data)
```
```{r}
?lme()
model_lme_clean <- lme(cbind(R5Hz_PP, R20Hz_PP, X5Hz_PP, Fres_PP) ~ BMI + Asthma + ICS + LABA + Gender + Age_months + Height_cm + Weight_Kg,
random = list(Subject_ID = pdIdent(~1)),
data = bmi_data_clean,
method = "REML")
```
```{r}
model_lmer_clean <- lmer(R5Hz_PP + R20Hz_PP + X5Hz_PP + Fres_PP ~ BMI + Asthma + ICS + LABA + Gender + Age_months + Height_cm + Weight_Kg + (1 | Subject_ID),
data = bmi_data_clean)
```
```{r}
AIC_lme <- AIC(model_lme_clean)
AIC_lmer <- AIC(model_lmer_clean)
cat("AIC for lme model:", AIC_lme, "\n")
cat("AIC for lmer model:", AIC_lmer, "\n")
if (AIC_lme < AIC_lmer) {
final_model <- model_lme_clean
cat("Final model selected: lme\n")
} else {
final_model <- model_lmer_clean
cat("Final model selected: lmer\n")
}
summary(final_model)
```
```{r}
residuals_clean <- resid(model_lme_clean)
```
```{r}
plot(predict(model_lme_clean), residuals_clean, xlab = "Fitted Values", ylab = "Residuals")
abline(h = 0, col = "red") # Add horizontal line at y = 0
title("Residuals vs. Fitted Values")
```
```{r}
hist(residuals_clean, main = "Histogram of Residuals")
# Q-Q plot
qqPlot(residuals_clean, main = "Q-Q Plot of Residuals")
```
```{r}
shapiro.test(residuals(model_lme_clean))
qqnorm(residuals(model_lme_clean))
qqline(residuals(model_lme_clean))
```
```{r}
fitted_values <- fitted(model_lme_clean)
residuals <- residuals(model_lme_clean)
plot(fitted_values, residuals, xlab = "Fitted values", ylab = "Residuals",
main = "Residuals vs Fitted Values Plot")
abline(h = 0, col = "red") # Add a horizontal line at y = 0 for reference
```