forked from sandialabs/MATLAB_PV_LIB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpvl_robustfit.m
70 lines (56 loc) · 2.01 KB
/
pvl_robustfit.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
function beta = pvl_robustfit(X, Y, intercept)
% PVL_ROBUSTFIT Regress Y onto X using iteratively reweighted least squares
%
% Syntax
% beta = pvl_robustfit(X, Y, intercept)
%
% Description
% PVL_ROBUSTFIT uses the Matlab function robustfit if the Statistics
% toolbox is available. Otherwise it uses the RLM function in the Python
% statsmodels package, which requires a Python installation.
%
% Inputs
% X - must be a N x M matrix, where M is the number of predictors and N is
% the number of observations
% Y - must be N x 1 vector
% intercept - an integer, 0 indicates no intercept, any other value
% indicates that the regression has an intercept term
%
% Output
% beta - a vector of coefficients, M x 1 if no intercept is specified,
% (M+1) x 1 if an intercept is specified. The coefficient for the
% intercept (if present) is beta(1).
% See if robustfit is available (requires statistics toolbox)
tmp = which('robustfit');
if isempty(tmp)
% need python code
msg = 'Statistics toolbox not found';
disp(msg);
% check for python installation
tmp2 = dos('python --version');
if tmp2==0
% found python
msg = 'Found Python installation; using statsmodels package';
disp(msg);
loc = which('py_rlm.py'); % find python script
tmpdir = getenv('TEMP');
f1 = fopen([tmpdir '\temp_rlm_config.txt'],'w');
fprintf(f1,'%i\n',intercept);
fclose(f1);
csvwrite([tmpdir '\temp_rlm_input.csv'],[X Y])
dos(['python ' loc]);
beta = csvread([tmpdir '\temp_rlm_output.csv']);
% clean up temporary files
delete([tmpdir '\temp_rlm_config.txt'], [tmpdir '\temp_rlm_input.csv'], [tmpdir '\temp_rlm_output.csv']);
else
msg = 'Python installation not found';
disp(msg);
beta = NaN;
end
else
if intercept
beta = robustfit(X,Y);
else
beta = robustfit(X,Y,'bisquare',4.685,'off'); % const is 'off' to omit the intercept.
end
end