-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnalyzeData.m
258 lines (174 loc) · 9.1 KB
/
AnalyzeData.m
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
%% Clear everything out
clc
fprintf('%s - Clearing everything out\n', datetime)
close all
clearvars
%% Set up the Import Options and import the data
opts = delimitedTextImportOptions("NumVariables", 20);
% Specify range and delimiter
opts.DataLines = [2, Inf];
opts.Delimiter = ",";
% Specify column names and types
opts.VariableNames = ["StartPeriod", "EndPeriod", "Var3", "Var4", "Var5", "Var6", "Var7", "Var8", "Var9", "Var10", "Var11", "Var12", "Var13", "Var14", "Var15", "Var16", "Var17", "Var18", "Var19", "Var20"];
opts.SelectedVariableNames = ["StartPeriod", "EndPeriod"];
opts.VariableTypes = ["datetime", "datetime", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string", "string"];
% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
% Specify variable properties
opts = setvaropts(opts, ["Var3", "Var4", "Var5", "Var6", "Var7", "Var8", "Var9", "Var10", "Var11", "Var12", "Var13", "Var14", "Var15", "Var16", "Var17", "Var18", "Var19", "Var20"], "WhitespaceRule", "preserve");
opts = setvaropts(opts, ["Var3", "Var4", "Var5", "Var6", "Var7", "Var8", "Var9", "Var10", "Var11", "Var12", "Var13", "Var14", "Var15", "Var16", "Var17", "Var18", "Var19", "Var20"], "EmptyFieldRule", "auto");
opts = setvaropts(opts, "StartPeriod", "InputFormat", "dd-MMM-yyyy HH:mm:ss");
opts = setvaropts(opts, "EndPeriod", "InputFormat", "dd-MMM-yyyy HH:mm:ss");
% Import the data
PeriodData = readtable("AggregatedData.csv", opts);
PeriodData = PeriodData{:,:}; % turn in to datetime matrix
%% Clear temporary variables
clear opts
%% Set up the Import Options and import the data
opts = delimitedTextImportOptions("NumVariables", 20);
% Specify range and delimiter
opts.DataLines = [2, Inf];
opts.Delimiter = ",";
% Specify column names and types
opts.VariableNames = ["Var1", "Var2", "StartWeightlbs", "StartFastingGlucosemgdL", "StartFastingKetonesmmolL", "ActiveEnergycal", "BasalEnergycal", "Sleepsec", "NutritionEnergycal", "TotalCarbsg", "Proteing", "TotalFatsg", "Sugarg", "Fiberg", "SugarAlcoholsg", "Bias", "EndWeightlbs", "WeightDifferencelbs", "EndFastingGlucosemgdL", "EndFastingKetonesmmolL"];
opts.SelectedVariableNames = ["StartWeightlbs", "StartFastingGlucosemgdL", "StartFastingKetonesmmolL", "ActiveEnergycal", "BasalEnergycal", "Sleepsec", "NutritionEnergycal", "TotalCarbsg", "Proteing", "TotalFatsg", "Sugarg", "Fiberg", "SugarAlcoholsg", "Bias", "EndWeightlbs", "WeightDifferencelbs", "EndFastingGlucosemgdL", "EndFastingKetonesmmolL"];
opts.VariableTypes = ["string", "string", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double"];
% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
% Specify variable properties
opts = setvaropts(opts, ["Var1", "Var2"], "WhitespaceRule", "preserve");
opts = setvaropts(opts, ["Var1", "Var2"], "EmptyFieldRule", "auto");
% Import the data
AggregatedData = readtable("AggregatedData.csv", opts);
%% Convert to output type
AggregatedData = table2array(AggregatedData);
%% Clear temporary variables
clear opts
%% Compute a linear regression model and cross-validate with k-folds
features = {"StartWt"; % 1
"Glucose"; % 2
"Ketones"; % 3
"ActEnrg"; % 4
"BslEnrg"; % 5
"Sleep "; % 6
"NutrCal"; % 7
"TotCarb"; % 8
"Protein"; % 9
"TotFats"; % 10
"Sugar "; % 11
"Fiber "; % 12
"SugAlc "; % 13
"Bias "; % 14
"EndWeight "; % 15
"WeightDiff"; % 16
"Glucose "; % 17
"Ketones "}; % 18
K = 10;
N = length(AggregatedData);
%% Linear regression with unnormalized data and no bias term
fprintf("**** LINEAR REGRESSION\tTARGET WEIGHT=ABSOLUTE\tNORMALIZATION=FALSE\tBIAS=FALSE ****\n\n")
inp = [1:5 7:13];
outp = [15 17:18];
x = AggregatedData(:,inp);
y = AggregatedData(:,outp);
[b,mae,rmse,mse] = LinearRegressionWithKFolds(x, y, K, features(inp), features(outp), false);
%% Linear regression with unnormalized data and no bias term
fprintf("**** LINEAR REGRESSION\tTARGET WEIGHT=DELTA\tNORMALIZATION=FALSE\tBIAS=FALSE ****\n\n")
inp = [2:5 7:13];
outp = [16:18];
x = AggregatedData(:,inp);
y = AggregatedData(:,outp);
[b,mae,rmse,mse] = LinearRegressionWithKFolds(x, y, K, features(inp), features(outp), false);
%% Linear regression with unnormalized data and no bias term
fprintf("**** LINEAR REGRESSION\tTARGET WEIGHT=BOTH\tNORMALIZATION=FALSE\tBIAS=FALSE ****\n\n")
inp = [1:5 7:13];
outp = [15:18];
x = AggregatedData(:,inp);
y = AggregatedData(:,outp);
[b,mae,rmse,mse] = LinearRegressionWithKFolds(x, y, K, features(inp), features(outp), false);
%% Linear regression with unnormalized data and bias term
fprintf("**** LINEAR REGRESSION\tTARGET WEIGHT=ABSOLUTE\tNORMALIZATION=FALSE\tBIAS=TRUE ****\n\n")
inp = [1:5 7:14];
outp = [15 17:18];
x = AggregatedData(:,inp);
y = AggregatedData(:,outp);
[b,mae,rmse,mse] = LinearRegressionWithKFolds(x, y, K, features(inp), features(outp), false);
%% Linear regression with unnormalized data and bias term
fprintf("**** LINEAR REGRESSION\tTARGET WEIGHT=DELTA\tNORMALIZATION=FALSE\tBIAS=TRUE ****\n\n")
inp = [2:5 7:14];
outp = [16:18];
x = AggregatedData(:,inp);
y = AggregatedData(:,outp);
[b,mae,rmse,mse] = LinearRegressionWithKFolds(x, y, K, features(inp), features(outp), false);
%% Linear regression with unnormalized data and bias term
fprintf("**** LINEAR REGRESSION\tTARGET WEIGHT=BOTH\tNORMALIZATION=FALSE\tBIAS=TRUE ****\n\n")
inp = [1:5 7:14];
outp = [15:18];
x = AggregatedData(:,inp);
y = AggregatedData(:,outp);
[b,mae,rmse,mse] = LinearRegressionWithKFolds(x, y, K, features(inp), features(outp), false);
%% Normalize data
% features
NormalizedData(:,1) = AggregatedData(:,1) / 230; % starting weight
NormalizedData(:,2) = AggregatedData(:,2) / 110; % blood glucose
NormalizedData(:,3) = AggregatedData(:,3) / 5; % ketones
NormalizedData(:,4) = AggregatedData(:,4) / 2000; % active energy
NormalizedData(:,5) = AggregatedData(:,5) / 2500; % basal energy
NormalizedData(:,6) = AggregatedData(:,6) / 35000; % sleep
NormalizedData(:,7) = AggregatedData(:,7) / 6500; % calories
NormalizedData(:,8) = AggregatedData(:,8) / 400; % total carbs
NormalizedData(:,9) = AggregatedData(:,9) / 350; % protein
NormalizedData(:,10) = AggregatedData(:,10) / 450; % total fats
NormalizedData(:,11) = AggregatedData(:,11) / 175; % sugar
NormalizedData(:,12) = AggregatedData(:,12) / 125; % fiber
NormalizedData(:,13) = AggregatedData(:,13) / 125; % sugar alcohols
NormalizedData(:,14) = AggregatedData(:,14); % bias term doesn't need to be normalized
% targets
NormalizedData(:,15) = AggregatedData(:,15) / 230; % end weight
NormalizedData(:,16) = AggregatedData(:,16) / 10; % weight difference
NormalizedData(:,17) = AggregatedData(:,17) / 110; % blood glucose
NormalizedData(:,18) = AggregatedData(:,18) / 5; % ketones
%% Linear regression with normalized data and no bias term
fprintf("**** LINEAR REGRESSION\tTARGET WEIGHT=ABSOLUTE\tNORMALIZATION=TRUE\tBIAS=FALSE ****\n\n")
inp = [1:5 7:13];
outp = [15 17:18];
x = NormalizedData(:,inp);
y = NormalizedData(:,outp);
[b,mae,rmse,mse] = LinearRegressionWithKFolds(x, y, K, features(inp), features(outp), false);
%% Linear regression with normalized data and no bias term
fprintf("**** LINEAR REGRESSION\tTARGET WEIGHT=DELTA\tNORMALIZATION=TRUE\tBIAS=FALSE ****\n\n")
inp = [2:5 7:13];
outp = [16:18];
x = NormalizedData(:,inp);
y = NormalizedData(:,outp);
[b,mae,rmse,mse] = LinearRegressionWithKFolds(x, y, K, features(inp), features(outp), false);
%% Linear regression with normalized data and no bias term
fprintf("**** LINEAR REGRESSION\tTARGET WEIGHT=BOTH\tNORMALIZATION=TRUE\tBIAS=FALSE ****\n\n")
inp = [1:5 7:13];
outp = [15:18];
x = NormalizedData(:,inp);
y = NormalizedData(:,outp);
[b,mae,rmse,mse] = LinearRegressionWithKFolds(x, y, K, features(inp), features(outp), false);
%% Linear regression with normalized data and bias term
fprintf("**** LINEAR REGRESSION\tTARGET WEIGHT=ABSOLUTE\tNORMALIZATION=TRUE\tBIAS=TRUE ****\n\n")
inp = [1:5 7:14];
outp = [15 17:18];
x = NormalizedData(:,inp);
y = NormalizedData(:,outp);
[b,mae,rmse,mse] = LinearRegressionWithKFolds(x, y, K, features(inp), features(outp), false);
%% Linear regression with normalized data and bias term
fprintf("**** LINEAR REGRESSION\tTARGET WEIGHT=DELTA\tNORMALIZATION=TRUE\tBIAS=TRUE ****\n\n")
inp = [2:5 7:14];
outp = [16:18];
x = NormalizedData(:,inp);
y = NormalizedData(:,outp);
[b,mae,rmse,mse] = LinearRegressionWithKFolds(x, y, K, features(inp), features(outp), false);
%% Linear regression with normalized data and bias term
fprintf("**** LINEAR REGRESSION\tTARGET WEIGHT=BOTH\tNORMALIZATION=TRUE\tBIAS=TRUE ****\n\n")
inp = [1:5 7:14];
outp = [15:18];
x = NormalizedData(:,inp);
y = NormalizedData(:,outp);
[b,mae,rmse,mse] = LinearRegressionWithKFolds(x, y, K, features(inp), features(outp), false);