-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLatent_Profile_Analysis.qmd
138 lines (90 loc) · 2.23 KB
/
Latent_Profile_Analysis.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
---
title: Latent Profile Analysis
format:
gfm:
toc: true
warning: false
fig-align: center
fig-cap-location: bottom
fig-width: 9
fig-height: 9
---
```{r}
#| include: false
library (reticulate)
path <- Sys.which("python")
path <- gsub("\\", "//", path, fixed = TRUE)
use_python(path)
```
## Using R (mclust package)
```{r}
# install.packages("mclust")
library(mclust)
library(tidyverse)
dat <- USArrests
dat |> dim()
dat |> head()
dat_scale <- dat |> scale() |> as.data.frame()
lpa_Model <- dat_scale |>
Mclust(G = 1:5)
lpa_Model |> summary()
lpa_assignments <- lpa_Model$classification
# Summarize the latent profiles
dat |>
scale() |>
as.data.frame() |>
mutate(Profile = lpa_assignments) |>
group_by(Profile) |>
summarize_all(mean)
plot(lpa_Model)
pred_Model_1 <- lpa_Model$classification
```
***
***
## Using R (tidyLPA package)
```{r}
# install.packages("tidyLPA")
n_profiles <- 3 # Specify the number of profiles
# Perform the LPA
lpa_Model2 <- dat |>
scale() |>
as.data.frame() |>
dplyr :: select(Murder, Assault, UrbanPop, Rape) |>
tidyLPA :: estimate_profiles(n_profiles, variances = "equal")
# Print the results
print(lpa_Model2)
```
***
***
***
***
## Using python (scikit-learn)
```{python}
# first in terminal:
# pip install -U scikit-learn
# Import the necessary libraries
import pandas as pd
import numpy as np
from sklearn.mixture import GaussianMixture
# Load the USArrests data
df = r.dat_scale
df.head()
# Perform the LPA using GaussianMixture
gmm = GaussianMixture(n_components=3)
gmm.fit(df)
# Print the results
print('Converged:',gmm.converged_) # Check if the model has converged
print('Means of each component:', gmm.means_)
print('Covariances of each component:', gmm.covariances_)
# Predict the labels for the data samples in df_scaled using gmm model
python_labels = gmm.predict(df)
py_labels = np.array(python_labels) + 1
R_labels = r.pred_Model_1
pd.DataFrame({"R_Labels": list(map(lambda x: int(x), R_labels)), "Python_Labels": py_labels})
```
***
***
## compare results
```{r}
adjustedRandIndex(py$R_labels, py$python_labels)
```