-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathglmTutorial.m
More file actions
175 lines (148 loc) · 4.61 KB
/
Copy pathglmTutorial.m
File metadata and controls
175 lines (148 loc) · 4.61 KB
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
function glmTutorial
%
% Demonstrate how to use Matlab's Statistics Toolbox glm routines
% to fit data.
%
% This is right basic idea, but needs a little fixing up still.
%
% Need to:
% a) Add better comments.
% b) Show how to wrap a parameter search around the parameters of
% the linking function.
% c) Worry about the regime where the linking function is such that
% the glm routines return NaN because of ill-conditioning.
%
% 9/21/13 dhb Wrote it.
%% Clear
clear; close all;
%% Parameters.
%
% We construct a linear function of some
% random numbers, bTrue gives the weights.
bTrue = [2 3 5]';
xDim = length(bTrue);
nObservations = 100;
noiseSd = 0.01;
%% Link function and its parameters.
%
% We assume that the observed data are a Naka-Rushton function
% of the linear values. The way the glm stuff works,
% this means that the linking function is the inverse of the
% Naka-Rushton function.
global linkParams
linkParams.type = 'AffinePower';
switch (linkParams.type)
case 'InverseNakaRushton'
linkParams.params(1) = 10;
linkParams.params(2) = 3;
linkParams.params(3) = 2;
linkS.Link = @ForwardLink;
linkS.Derivative = @DerivativeLink;
linkS.Inverse = @InverseLink;
case 'AffinePower'
linkParams.params(1) = 1;
linkParams.params(2) = 0.5;
linkS.Link = @ForwardLink;
linkS.Derivative = @DerivativeLink;
linkS.Inverse = @InverseLink;
case 'Power'
linkParams.params(1) = 2;
linkS.Link = @ForwardLink;
linkS.Derivative = @DerivativeLink;
linkS.Inverse = @InverseLink;
otherwise
error('Unknown link function type');
end
%% X variables
X = rand(nObservations,xDim);
%% Linear y is a linear function of X
yLinear = X*bTrue;
yNonLinear = InverseLink(yLinear);
yObserved = yNonLinear + noiseSd*randn(size(yNonLinear));
%% Figure
[~,index] = sort(yLinear);
theFig = figure; clf;
hold on
plot(yLinear(index),yObserved(index),'ro','MarkerSize',8,'MarkerFaceColor','r');
plot(yLinear(index),yNonLinear(index),'r');
%% GLM fit
warnState = warning('off','stats:glmfit:IterationLimit');
GLM = GeneralizedLinearModel.fit(X,yObserved,'Distribution','normal','Link',linkS,'Intercept',false);
warning(warnState);
bFit = GLM.Coefficients.Estimate
yLinearPred = X*bFit;
yNonLinearPred = InverseLink(yLinearPred);
figure(theFig);
plot(yLinear(index),yNonLinearPred(index),'b');
xlabel('True linear value');
ylabel('Obs/Predicted nonlinear value');
%% This is just a check that the PTB NakaRushton function properly
% inverts itself.
switch (linkParams.type)
case 'NakaRushton'
linearInvertCheck = ForwardLink(yNonLinearPred);
if (any(abs(yLinearPred-linearInvertCheck) > 1e-7))
error('Naka-Rushton inversion error');
end
% A little more testing
figure; clf;
derivCheck = DerivativeLink(yNonLinearPred);
subplot(2,1,1); hold on
plot(yNonLinearPred(index),linearInvertCheck(index),'r');
subplot(2,1,2); hold on
plot(yNonLinearPred(index),derivCheck(index),'r');
end
end
function out = ForwardLink(in)
global linkParams
switch (linkParams.type)
case 'InverseNakaRushton'
in(in < 0) = 0;
in(in > linkParams.params(1)) = linkParams.params(1);
out = InvertNakaRushton(linkParams.params,in);
case 'AffinePower'
in(in < 0) = 0;
out = linkParams.params(1) + in.^linkParams.params(2);
case 'Power'
in(in < 0) = 0;
out = in.^linkParams.params(1);
otherwise
error('Unknown link function type');
end
end
function out = DerivativeLink(in)
global linkParams
switch (linkParams.type)
case 'InverseNakaRushton'
in(in < 0) = 0;
in(in > linkParams.params(1)) = linkParams.params(1);
out = DerivativeInvertNakaRushton(linkParams.params,in);
case 'AffinePower'
in(in < 0) = 0;
out = linkParams.params(2)*in.^(linkParams.params(2)-1);
case 'Power'
in(in < 0) = 0;
out = linkParams.params(2)*in.^(linkParams.params(2)-1);
otherwise
error('Unknown link function type');
end
end
function out = InverseLink(in)
global linkParams
switch (linkParams.type)
case 'InverseNakaRushton'
% Force input into required range
in(in < 0) = 0;
out = ComputeNakaRushton(linkParams.params,in);
case 'AffinePower'
in(in < 0) = 0;
tmp = in - linkParams.params(1);
tmp(tmp < 0) = 0;
out = tmp.^(1/linkParams.params(2));
case 'Power'
in(in < 0) = 0;
out = in.^(1/linkParams.params(2));
otherwise
error('Unknown link function type');
end
end