Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
hendri54 committed Jun 6, 2016
1 parent 2bfccc8 commit fb3d0ab
Show file tree
Hide file tree
Showing 26 changed files with 4,222 additions and 8 deletions.
15 changes: 15 additions & 0 deletions +displayLH/time_str.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function timeStr = time_str(clockV)
% Return formatted string showing current time
% Or using time vector as returned by clock

if nargin < 1
clockV = clock;
end
if isempty(clockV)
clockV = clock;
end

timeStr = sprintf('%02i:%02i:%02i', round(clockV(4:6)));


end
83 changes: 83 additions & 0 deletions +figuresLH/common_axis_limits.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
function [xLimV, yLimV] = common_axis_limits(fhV, axisV)
% Given a set of figure (or subplot axis) handles, find the axis range that encompasses all
%{
IN:
fhV
figure or axis handles
axisV
fixed axis values (optional)
can contain NaN to be ignored
%}

%% Input check

n = length(fhV);

if ~isempty(axisV)
assert(length(axisV) == 4);
end


%% Get axis handles

if isa(fhV(1), 'matlab.ui.Figure')
% Figure handles
ahV = gobjects(1, n);
for i1 = 1 : n
ahV(i1) = get(fhV(i1), 'CurrentAxes');
end

elseif isa(fhV(1), 'double') || isa(fhV(1), 'matlab.graphics.axis.Axes')
% Axis handles
ahV = fhV;

else
error('Invalid');
end


%% Get limits

xLimV = zeros(1, 2);
yLimV = zeros(1, 2);

for i1 = 1 :n
xLimNewV = get(ahV(i1), 'xLim');
yLimNewV = get(ahV(i1), 'yLim');

if i1 > 1
% Keep the max
xLimV(1) = min(xLimV(1), xLimNewV(1));
xLimV(2) = max(xLimV(2), xLimNewV(2));
yLimV(1) = min(yLimV(1), yLimNewV(1));
yLimV(2) = max(yLimV(2), yLimNewV(2));
else
xLimV = xLimNewV;
yLimV = yLimNewV;
end
end


%% Override limits

% Override with target values
if ~isempty(axisV)
if ~isnan(axisV(1))
xLimV(1) = axisV(1);
end
if ~isnan(axisV(2))
xLimV(2) = axisV(2);
end
if ~isnan(axisV(3))
yLimV(1) = axisV(3);
end
if ~isnan(axisV(4))
yLimV(2) = axisV(4);
end
end


validateattributes(xLimV, {'double'}, {'finite', 'nonnan', 'nonempty', 'real', 'size', [1,2]})
validateattributes(yLimV, {'double'}, {'finite', 'nonnan', 'nonempty', 'real', 'size', [1,2]})

end
51 changes: 51 additions & 0 deletions +figuresLH/figure_axes_same.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
function figure_axes_same(fhV, axisV)
% Set all figure axes the same
%{
Set the max axis range of all figures
axisV overrides that
IN
fhV
vector of figure handles (for separate figures)
axisV (optional)
desired axis ranges
%}

%% Input check
if ~isempty(axisV)
if length(axisV) ~= 4
error('Invalid axisV');
end
end

if ~isa(fhV(1), 'matlab.ui.Figure')
error('Invalid');
end


%% Find max axis dimensions of all figures

[xLimV, yLimV] = figuresLH.common_axis_limits(fhV, axisV);
axisValV = [xLimV, yLimV];
validateattributes(axisValV, {'double'}, {'finite', 'nonnan', 'nonempty', 'real', 'size', [1,4]})


%% Set axes

% if ~isempty(axisV)
% idxV = find(~isnan(axisV));
% if ~isempty(idxV)
% axisValV(idxV) = axisV(idxV);
% end
% end

for i1 = 1 : length(fhV)
%figure(fhV(i1));
%axis(gca, axisValV);
ah = get(fhV(i1), 'CurrentAxes');
xlim(ah, axisValV(1:2));
ylim(ah, axisValV(3:4));
end


end
86 changes: 86 additions & 0 deletions +figuresLH/subplot_axes_same.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
function subplot_axes_same(fhV, axisV)
% Set all subplot axes the same
%{
Set the max axis range of all figures
axisV overrides that
IN
fhV
vector of axis handles
axisV (optional)
desired axis ranges
%}

%% Input check
if ~isempty(axisV)
if length(axisV) ~= 4
error('Invalid axisV');
end
end

if ~isa(fhV(1), 'matlab.graphics.axis.Axes') && ~isa(fhV(1), 'double')
error('Invalid');
end


%% Main

% Get max limits
[xLimV, yLimV] = figuresLH.common_axis_limits(fhV, axisV);

% % Override with target values
% if ~isempty(axisV)
% if ~isnan(axisV(1))
% xLimV(1) = axisV(1);
% end
% if ~isnan(axisV(2))
% xLimV(2) = axisV(2);
% end
% if ~isnan(axisV(3))
% yLimV(1) = axisV(3);
% end
% if ~isnan(axisV(4))
% yLimV(2) = axisV(4);
% end
% end

xlim(fhV(1), xLimV);
ylim(fhV(1), yLimV);

% % Override
% if ~isempty(axisV)
% % Compute new x limits
% xLimV = set_new_limits(axisV(1:2), get(fhV(1), 'xLim'));
% if ~isempty(xLimV)
% for i1 = 1 : length(fhV)
% xlim(fhV(i1), xLimV);
% end
% end
%
% % Compute new y limits
% yLimV = set_new_limits(axisV(3:4), get(fhV(1), 'yLim'));
% if ~isempty(yLimV)
% for i1 = 1 : length(fhV)
% ylim(fhV(i1), yLimV);
% end
% end
% end


linkaxes(fhV);



end


% %% Set new axis limits
% function outV = set_new_limits(tgV, currentV)
% idxV = find(~isnan(tgV));
% if isempty(idxV)
% outV = [];
% else
% outV = currentV;
% outV(idxV) = tgV(idxV);
% end
% end
13 changes: 13 additions & 0 deletions +matrixLH/array2cell.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function outM = array2cell(dataM, fmtStr)
% Convert a numeric array (dataM) to a cell array of formatted strings
%{
IN:
fmtStr
format string for sprintf
%}
% ------------------------------------------

f1 = @(x) sprintf(fmtStr, x);
outM = cellfun(f1, num2cell(dataM), 'UniformOutput', false);

end
13 changes: 13 additions & 0 deletions +randomLH/rand_time.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function out1 = rand_time
% Generate a uniform random variable from the current time
%{
Does not disturb the random seed
This is a bit slow
%}

x = rng;
rng('shuffle');
out1 = rand(1,1);
rng(x);

end
18 changes: 18 additions & 0 deletions +randomLH/rand_time_test.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function rand_time_test

rng(45);
y0 = rand(1,1);

rng(45);
x1 = randomLH.rand_time;
y1 = rand(1,1);

% Did not change seed
assert(abs(y0 - y1) < 1e-8);

% Get different values each time
x2 = randomLH.rand_time;
assert(abs(x2 - x1) > 1e-8);


end
104 changes: 104 additions & 0 deletions +regressLH/RegrAgeSchoolYear.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
% Regress a variable on age / school / year dummies
%{
%}
classdef RegrAgeSchoolYear

properties
weighted logical
ageRangeV uint8
yearRangeV uint16
schoolRangeV uint8
end


methods
%% Constructor
function rS = RegrAgeSchoolYear(ageRangeV, schoolRangeV, yearRangeV, weighted)
rS.weighted = weighted;
validateattributes(ageRangeV(:), {'numeric'}, {'finite', 'nonnan', 'nonempty', 'integer', 'positive', ...
'<', 120, 'size', [2,1]})
rS.ageRangeV = uint8(ageRangeV(:));

validateattributes(schoolRangeV(:), {'numeric'}, {'finite', 'nonnan', 'nonempty', 'integer', '>=', 0, ...
'<', 35, 'size', [2,1]})
rS.schoolRangeV = uint8(schoolRangeV(:));

validateattributes(yearRangeV(:), {'numeric'}, {'finite', 'nonnan', 'nonempty', 'integer', '>', 1600, ...
'<', 2050, 'size', [2,1]})
rS.yearRangeV = uint16(yearRangeV(:));
end


%% Regression
%{
OUT
ageDummy, schoolDummyV, yearDummyV
meaningless scales
mdl
linear model with regressors: age, school, year, x
can be used to get predicted wages easily:
feval(mdl, [40, 12, 1998, 1.234])
%}
function outS = regress(rS, y_astM, x_astM, wt_astM)
nAge = diff(rS.ageRangeV) + 1;
nSchool = diff(rS.schoolRangeV) + 1;
nYear = diff(rS.yearRangeV) + 1;

valid_astM = ~isnan(y_astM);
if rS.weighted
valid_astM(wt_astM <= 0) = false;
end
if ~isempty(x_astM)
valid_astM(isnan(x_astM)) = false;
end
vIdxV = find(valid_astM(:));
nObs = length(vIdxV);

ageV = rS.ageRangeV(1) : rS.ageRangeV(2);
a_astM = repmat(ageV(:), [1, nSchool, nYear]);
assert(isequal(size(a_astM), size(y_astM)));
assert(all(abs(a_astM(:,2,2) - ageV(:)) < 1e-8));

schoolV = rS.schoolRangeV(1) : rS.schoolRangeV(2);
s_astM = permute(repmat(schoolV(:), [1, nAge, nYear]), [2, 1, 3]);
assert(isequal(size(s_astM), size(y_astM)));
assert(all(abs(squeeze(s_astM(2,:,2)) - schoolV) < 1e-8));

yearV = rS.yearRangeV(1) : rS.yearRangeV(2);
t_astM = permute(repmat(yearV(:), [1, nAge, nSchool]), [2, 3, 1]);
assert(isequal(size(t_astM), size(y_astM)));
assert(all(abs(squeeze(t_astM(2,2,:)) - yearV(:)) < 1e-8));


% **** Regression

xM = [double(a_astM(vIdxV)), double(s_astM(vIdxV)), double(t_astM(vIdxV))];
if ~isempty(x_astM)
xM = [xM, x_astM(vIdxV)];
end

if rS.weighted
outS.mdl = fitlm(double(xM), double(y_astM(vIdxV)), 'Weights', wt_astM(vIdxV), 'CategoricalVars', [1,2,3]);
else
outS.mdl = fitlm(double(xM), double(y_astM(vIdxV)), 'CategoricalVars', [1,2,3]);
end


% **** Recover dummies

x2M = repmat(xM(1,:), [nAge, 1]);
x2M(:,1) = ageV;
outS.ageDummyV = feval(outS.mdl, x2M);

x2M = repmat(xM(1,:), [nSchool, 1]);
x2M(:,2) = schoolV;
outS.schoolDummyV = feval(outS.mdl, x2M);

x2M = repmat(xM(1,:), [nYear, 1]);
x2M(:,3) = yearV;
outS.yearDummyV = feval(outS.mdl, x2M);
end
end


end
Loading

0 comments on commit fb3d0ab

Please sign in to comment.