# A Study on ML-Based Sleep Score Model Using Lifelog Data
## 2 Materials and Methods
### Data Preparation and Preprocessing  
The data used in this study is set out in Table 1.  
The data were collected from 714 people from 26 November 2020 to 1 January 2022 by a Samsung Galaxy. Specifically, daily/minute sleep data, daily/by-minute step data, and user information (age, gender) were included. The collected data were preprocessed as daily data aggregation, sleep-related feature generation, outlier processing, and missing values. In daily data aggregation, features are generated by daily aggregation of sleep data collected per minute. Based on the study findings that sleep phase information for the initial 90 min of sleep indicates the quality of sleep, we created the sleep phase features for the first 90 min [14; 15]. We also generated total daily sleep stage ratio features (REM stage, light stage, deep stage, awake stage) [16; 17], sleep efficiency feature [18], SRI (sleep regularity index) [19; 20], and so on. The SRI feature is calculated through the SRI calculation Equation (3) assuming Equations (1) and (2) for \(M\) daily epochs and \(N\) days [20]:  
\[S_{i,j}=S_{i+1,\ j}\ \rightarrow\ \delta\big{(}S_{i,j},S_{i+1,\ j}\big{)}=1, \tag{1}\]  
\[S_{i,j}\
eq S_{i+1,\ j}\ \rightarrow\ \delta\big{(}S_{i,j},S_{i+1,\ j}\big{)}=0, \tag{2}\]  
\[-100+\frac{200}{M(N-1)}\sum_{j=1}^{M}\sum_{i=1}^{N-1}\delta\big{(}S_{i,j},\ S_{i+1,\ j}\big{)}. \tag{3}\]  
where \(N\) and \(M\) are the number of days and the number of epochs per day, respectively. The function \(\delta\) returns value 1 if the sleep occurrence (sleep-wake state) is the same at 24 h intervals and returns 0 otherwise. For example, if sleep occurred at 22:00 and ended at 06:00 on Friday and occurred at 22:30 and ended at 08:00 on Saturday, the function \(\delta\) from 22:30 to 06:00 is 1 and the rest of the time zone is 0. Daily sleep data are used to generate total sleep time [21] and sleep midpoint features [22]. In addition, to generate features for the step information just before sleep, the step data collected per minute is preprocessed and used together with the sleep data to generate features. Some of the feature names and descriptions are summarized in Table 2, and the rest are shown in Appendix A Table A1 for readability. \begin{table}
\begin{tabular}{c c c} \hline \hline
**Category** & **Value** & **Description** \\ \hline Quantity of sleep data collected & 67,180 rows & Sleep data set collected by day with \\ by day & & Samsung Galaxy 4 or 5 \\ Quantity of sleep data collected & & Sleep data set collected by day with \\ by minute & 2,494,862 rows & Samsung Galaxy 4 or 5 \\ Quantity of step (gait) data & 78,643 rows & Step data set collected by day with \\ collected by day & & Samsung Galaxy Watch 4 or 5 \\ Quantity of step data collected & 18,710,423 rows & Step data set collected by day with \\ by minute & 18,710,423 rows & Samsung Galaxy 4 or 5 \\ Quantity of user information data & 918 rows & User information such as height \\ Number of users & 714 & and age \\ Period of data collection & 26 November 2020 to \\  & 1 January 2022 & \\ \hline \hline \end{tabular}
\end{table} 
Table 1: Description of raw data collected from wearable devices (Samsung Galaxy watch). Outlier processing proceeds as follows:  
1. Data with a sleep stage value of 0 among the generated sleep stage features. 2. Less than 3 h of total sleep per day, since it does not record stages if the sleep is less than 3 h.
3. SRI index with negative values [23]. Missing-value processing based on sleep habit score will be described in detail after Section 2.2, but it is briefly described in this section as it is included in the overall preprocessing. Missing-value processing is organized into three steps as follows:  
1. Set the sleep habit score derived by the logistic regression model as the target and set the related sleep variable as the explanatory variable. (This is detailed in Section 2.2.1.) 2. Process missing values based on the KNN (K-nearest neighbors) machine learning algorithm [24; 25].