-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidateModel.m
80 lines (68 loc) · 2.36 KB
/
validateModel.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
function validateModel()
%validateModel produces R^2 and MSE calculations for regression models
BHatOLS = csvread('BHatOLS.csv');
BHatModifiedOLS = csvread('BHatModifiedOLS.csv');
BHatRidge = csvread('BHatRidge.csv');
BHatLasso = csvread('BHatLasso.csv');
G = csvread('ValidationG.csv');
Y = csvread('ValidationY.csv');
YHatOLS = calculateYHat(G, BHatOLS, 'OLS');
YHatModifiedOLS = calculateYHat(G, BHatModifiedOLS, 'ModifiedOLS');
YHatRidge = calculateYHat(G, BHatRidge, 'Ridge');
YHatLasso = calculateYHat(G, BHatLasso, 'Lasso');
RSquaredOLS = calculateRSquared(Y, YHatOLS);
RSquaredModifiedOLS = calculateRSquared(Y, YHatModifiedOLS);
RSquaredRidge = calculateRSquared(Y, YHatRidge);
RSquaredLasso = calculateRSquared(Y, YHatLasso);
MSEOLS = calculateMSE(Y, YHatOLS);
MSEModifiedOLS = calculateMSE(Y, YHatModifiedOLS);
MSERidge = calculateMSE(Y, YHatRidge);
MSELasso = calculateMSE(Y, YHatLasso);
printRSquaredAndMSE(RSquaredOLS, MSEOLS, 'OLS');
printRSquaredAndMSE(RSquaredModifiedOLS, MSEModifiedOLS, 'ModifiedOLS');
printRSquaredAndMSE(RSquaredRidge, MSERidge, 'Ridge');
printRSquaredAndMSE(RSquaredLasso, MSELasso, 'Lasso');
end
function YHat = calculateYHat(G, BHat, model)
%calculateYHat calculates YHat
%Args:
% G: n x m matrix of genotypes
% BHat: vector of m beta hats
% model: string describing model ('OLS', 'Ridge', 'Lasso', ...)
%Returns:
% YHat: vector of n predicted phenotypes
YHat = G * BHat;
dlmwrite(strcat('YHat', model, '.csv'), YHat);
end
function RSquared = calculateRSquared(Y, YHat)
%calculateRSquared calculates R^2 between Y and YHat
%Args:
% Y: vector of n true phenotypes
% YHat: vector of n predicted phenotypes
%Returns:
% RSquared: R^2
YResid = Y - YHat;
SSResid = sum(YResid.^2);
SStotal = (length(Y) - 1) * var(Y);
RSquared = 1 - SSResid/SStotal;
end
function MSE = calculateMSE(Y, YHat)
%calculateMSE calculates mean squared error (MSE) of YHat
%Args:
% Y: vector of n true phenotypes
% YHat: vector of n predicted phenotypes
%Returns:
% MSE: mean squared error
YResid = Y - YHat;
SSResid = sum(YResid.^2);
MSE = SSResid/length(Y);
end
function printRSquaredAndMSE(RSquared, MSE, model)
%printRSquaredAndMSE prints RSquared and MSE
%Args:
% RSquared: R^2
% MSE: mean squared error
% model: model: string describing model ('OLS', 'Ridge', 'Lasso', ...)
fprintf('%s RSquared = %f\n', model, RSquared);
fprintf('%s MSE = %f\n', model, MSE);
end