Plot q5, q50 and q95 in Matlab - matlab

I computed the quantiles at 5%, 50% and 95% with the command quantile(x, p). Now I would like to print these values with the boxplot or something that is appealing and clear. I found the boxplot function, but it prints only the 25%, 50% (the mean) and the 95%. Can I print the quantiles as I said before?

A somewhat inelegant solution would be to create the "boxes" yourself.
For example:
% Create the quantile arrays
N = 10; % number of points
x = 1:N;
q5 = rand(N, 1)./10; % 5% quantile
q50 = rand(N, 1); % 50% quantile
q95 = rand(N, 1).*10; % 95% quantile
% Create boxes
figure
hold on
w = 0.2; % width of the boxes
for i = 1:N
plot(x(i) + [-0.5, 0.5].*w, [q5(i), q5(i)], '-b', 'LineWidth', 2) % bottom of box
plot(x(i) + [0.5, 0.5].*w, [q5(i), q95(i)], '-b', 'LineWidth', 2) % right side
plot(x(i) + [-0.5, 0.5].*w, [q95(i), q95(i)], '-b', 'LineWidth', 2) % top of box
plot(x(i) + [-0.5, -0.5].*w, [q95(i), q5(i)], '-b', 'LineWidth', 2) % left side
plot(x(i) + [-0.5, 0.5].*w, [q50(i), q50(i)], '-r', 'LineWidth', 2) % mean
end
Just as simply, you could also add the whiskers.

Related

PCA of Ovarian Cancer Data via SVD

I want to analyze the Ovarian Cancer Data provided by MATLAB with the PCA. Specifically, I want to visualize the two largest Principal Components, and draw the two corresponding left singular vectors. As I understand, those vectors should be able to serve as a new coordinate-system, aligned towards the largest variance in the data. What I ultimately want to examine is if the cancer patients are distinguishable from the non-cancer patients.
Something that is still wrong in my script are the left singular vectors. They are not in a 90 degree angle to each other, and if I scale them by the respective eigenvalues, they explode in length. What am I doing wrong?
%% PCA - Ovarian Cancer Data
close all;
clear all;
% obs is an NxM matrix, where ...
% N = patients (216)
% M = features - genes in this case (4000)
load ovariancancer.mat;
% Turn obs matrix, such that the rows represent the features
X = obs.';
[U, S, V] = svd(X, 'econ');
% Crop U, S and V, to visualize two largest principal components
U_crop = U(:, 1:2);
S_crop = S(1:2, 1:2);
V_crop = V(:, 1:2);
X_crop = U_crop * S_crop * V_crop.';
% Average over cancer patients
xC = mean(X_crop, 2);
% Visualize two largest principal components as a data cloud
figure;
hold on;
for i = 1 : size(X, 2)
if grp{i} == 'Cancer'
plot(X_crop(1, i), X_crop(2, i), 'rx', 'LineWidth', 2);
else
plot(X_crop(1, i), X_crop(2, i), 'bo', 'LineWidth', 2);
end
end
%scatter(X_crop(1, :), X_crop(2, :), 'k.', 'LineWidth', 2)
set(gca,'DataAspectRatio',[1 1 1])
xlabel('PC1')
ylabel('PC2')
grid on;
Xstd = U_crop; % * S_crop?
quiver([xC(1) xC(1)], [xC(2) xC(2)], Xstd(1, :), Xstd(2, :), 'green', 'LineWidth', 3);
So there were multiple mistakes in my script. In case anyone is interested, I am posting the corrected code (I am plotting three PCs now). This post was very helpful.
% obs is an NxM matrix, where ...
% N = patients (216)
% M = features - genes in this case (4000)
load ovariancancer.mat;
% Let the data matrix X be of n×p size, where n is the number of samples and p is the number of variables
X = obs;
% Let us assume that it is centered, i.e. column means have been subtracted and are now equal to zero
Xavg = mean(X, 2);
%X = X - Xavg * ones(1, size(X, 2));
[U, S, V] = svd(X, 'econ');
PC = U * S;
% Visualize three largest principal components as a data cloud
% The j-th principal component is given by j-th column of XV. The coordinates of the i-th data point in the new PC space are given by the i-th row of XV
figure;
for i = 1 : size(PC, 2)
if grp{i} == 'Cancer'
plot3(PC(i, 1), PC(i, 2), PC(i, 3), 'rx', 'LineWidth', 2);
else
plot3(PC(i, 1), PC(i, 2), PC(i, 3), 'bo', 'LineWidth', 2);
end
hold on;
end
set(gca,'DataAspectRatio',[1 1 1])
xlabel('PC1')
ylabel('PC2')
zlabel('PC3')

Shaded plot in Matlab

I would like to plot a function in Matlab with a shaded area indicating the uncertainty over it (e.g., confidence interval). This can be achieved by using the fill function to create a color patch. For example
x = linspace(0, 2*pi, 100);
f = cos(x);
fUp = cos(x) + 1;
fLow = cos(x) - 1;
x2 = [x, fliplr(x)];
plot(x, f, 'k')
hold on
fill(x2, [f, fliplr(fUp)], 0.7 * ones(1, 3), 'linestyle', 'none', 'facealpha', 0.4);
fill(x2, [fLow, fliplr(f)], 0.7 * ones(1, 3), 'linestyle', 'none', 'facealpha', 0.4);
This creates a shaded gray area between the functions fLow and fUp, with f in the middle represented as a solid black line, as in the picture below.
I would like now to have the shaded area degrade its color when we approach the lower (resp. upper) bound of the confidence interval. In particular, I would like that while approaching its boundaries, the shaded area gets brighter and brighter. Is there a way to do it?
I'm doing two separate patches because I think it may be necessary for my purpose.
You can split your CI into n subarea:
x = linspace(0, 2*pi, 100);
f = cos(x);
n = 20; % step number
g = 0.3; % grayscale intensity
fUp = cos(x) + linspace(0,1,n).';
fLow = cos(x) - linspace(0,1,n).';
x2 = [x, fliplr(x)];
plot(x, f, 'k')
hold on
fill(x2, [repmat(f,n,1), fliplr(fUp)], g * ones(1, 3), 'linestyle', 'none', 'facealpha', [1/n]);
fill(x2, [fLow, repmat(fliplr(f),n,1)], g * ones(1, 3), 'linestyle', 'none', 'facealpha', [1/n]);
Which produce:
The subarea are overlapping and produce a maximum facealpha of n*(1/n) * g = g
Noticed that this method is not really memory efficient (since it produce n subarea on each side) and will only works with a linear shading.
If your CI is non linear then you should adjust this part:
% Prediction Linear CI
% ↓ ↓
cos(x) + linspace(0,1,n).';
cos(x) - linspace(0,1,n).';
to
% Prediction Non linear CI
% ↓ ↓
cos(x) + your_non_linear_CI_distribution;
cos(x) - your_non_linear_CI_distribution;

Octave: How can I fit a sinusoid to my data using Octave?

My goal is to fit a sinusoid to data goming from a datalogger using Octave.
The datalogger logs force which is produced using an excenter, so it theoretically should be a sine wave.
I could not find any hint on how to do this elsewhere.
Currently I'm using the function "splinefit" followd by "ppval" to fit my data but I don't realy get the results I hoped from it...
Has anybody an idea how I could fit a sinusoid to my data?
Here's my current code I use to fit the data and a scrennshot of the result:
## splinefit force left
spfFL = splinefit(XAxis,forceL,50);
fitForceL=ppval(spfFL,XAxis);
##middle force left
meanForceL=mean(fitForceL);
middleedForceL=fitForceL-meanForceL;
result spline fit
on the X-Axis I have the 30'000 measurepoints or logs
on the Y-Axis I have the actual measured force values
the data comes from the datalogger in a .csv-file like this
You can do a simple regression using the sine and cosine of your (time) input as your regression features.
Here's an example
% Let's generate a dataset from a known sinusoid as an example
N = 1000;
Range = 100;
w = 0.25; % known frequency (e.g. from specs or from fourier analysis)
Inputs = randi(Range, [N, 1]);
Targets = 0.5 * sin( w * Inputs + pi/3 ) + 0.05 * randn( size( Inputs ) );
% Y = A + B sin(wx) + C cos(wx); <-- this is your model
Features = [ ones(N, 1), sin(w * Inputs), cos(w * Inputs) ];
Coefs = pinv(Features) * Targets;
A = Coefs(1); % your solutions
B = Coefs(2);
C = Coefs(3);
% print your nice solution against the input dataset
figure('position', [0, 0, 800, 400])
ax1 = axes()
plot(Inputs, Targets, 'o', 'markersize', 10, ...
'markeredgecolor', [0, 0.25, 0.5], ...
'markerfacecolor', [0, 0.5, 1], ...
'linewidth', 1.5)
set(ax1, 'color', [0.9, 0.9, 0.9])
ax2 = axes()
X = 1:0.1:Range;
plot( X, A + B*sin(w * X) + C*cos(w * X), 'k-', 'linewidth', 5 ); hold on
plot( X, A + B*sin(w * X) + C*cos(w * X), 'g-', 'linewidth', 2 ); hold off
set(ax2, 'xlim', get(ax1, 'xlim'), 'ylim', get(ax1, 'ylim'), 'color', 'none')
You could do a least squares optimization, using fminsearch
% sine to fit (in your case your data)
x = 0:0.01:50;
y = 2.6*sin(1.2*x+3.1) + 7.3 + 0.2*rand(size(x)); % create some noisy sine with known parameters
% function with parameters
fun = #(x,p) p(1)*sin(p(2)*x+p(3)) + p(4); % sine wave with 4 parameters to estimate
fcn = #(p) sum((fun(x,p)-y).^2); % cost function to minimize the sum of the squares
% initial guess for parameters
p0 = [0 0 0 0];
% parameter optimization
par = fminsearch(fcn, p0);
% see if estimated parameters match measured data
yest = fun(x, par)
plot(x,y,x,yest)
Replace x and y with your data. The par variable contains the parameters of the sine, as defined in fun.

MATLAB - Smooth heat map from (x, y, z) points within a triangle?

I have many 3D scatter points (x, y, z) that are guaranteed to be within a triangle. I now wish to visualize z as one smooth 2D heat map, where positions are given by (x, y).
I can easily do it with meshgrid and mesh, if (x, y) together form a rectangle. Because I don't want anything falling outside of my triangle, I can't use griddate either.
Then how?
MWE
P = [0 1/sqrt(3); 0.5 -0.5/sqrt(3); -0.5 -0.5/sqrt(3)];
% Vertices
scatter(P(:, 1), P(:, 2), 100, 'ro');
hold on;
% Edges
for idx = 1:size(P, 1)-1
plot([P(idx, 1) P(idx+1, 1)], [P(idx, 2) P(idx+1, 2)], 'r');
end
plot([P(end, 1) P(1, 1)], [P(end, 2) P(1, 2)], 'r');
% Sample points within the triangle
N = 1000; % Number of points
t = sqrt(rand(N, 1));
s = rand(N, 1);
sample_pts = (1-t)*P(1, :)+bsxfun(#times, ((1-s)*P(2, :)+s*P(3, :)), t);
% Colors for demo
C = ones(size(sample_pts, 1), 1).*sample_pts(:, 1);
% Scatter sample points
scatter(sample_pts(:, 1), sample_pts(:, 2), [], C, 'filled');
colorbar;
produces
PS
As suggested by Nitish, increasing number of points will do the trick. But is there a more computationally cheap way of doing so?
Triangulate your 2D data points using delaunayTriangulation, evaluate your function with the points of the triangulation and then plot the resulting surface using trisurf:
After %Colors for demo, add this:
P = [P; sample_pts]; %// Add the edgepoints to the sample points, so we get a triangle.
f = #(X,Y) X; %// Defines the function to evaluate
%// Compute the triangulation
dt = delaunayTriangulation(P(:,1),P(:,2));
%// Plot a trisurf
P = dt.Points;
trisurf(dt.ConnectivityList, ...
P(:,1), P(:,2), f(P(:,1),P(:,2)), ...
'EdgeColor', 'none', ...
'FaceColor', 'interp', ...
'FaceLighting', 'phong');
%// A finer colormap gives more beautiful results:
colormap(jet(2^14)); %// Or use 'parula' instead of 'jet'
view(2);
The trick to make this graphic beautiful is to use 'FaceLighting','phong' instead of 'gouraud' and use a denser colormap than is usually used.
The following uses only N = 100 sample points, but a fine colormap (using the now default parula colormap):
In comparison the default output for:
trisurf(dt.ConnectivityList, ...
P(:,1), P(:,2), f(P(:,1),P(:,2)), ...
'EdgeColor', 'none', ...
'FaceColor', 'interp');
looks really ugly: (I'd say mainly because of the odd interpolation, but the jet colormap also has its downsides)
Why not just increase N to make the grid "more smooth"? It will obviously be more computationally expensive but is probably better than extrapolation. Since this is a simulation where s and t are your inputs, you can alternately create a fine grids for them (depending on how they interact).
P = [0 1/sqrt(3); 0.5 -0.5/sqrt(3); -0.5 -0.5/sqrt(3)];
% Vertices
scatter(P(:, 1), P(:, 2), 100, 'ro');
hold on;
% Edges
for idx = 1:size(P, 1)-1
plot([P(idx, 1) P(idx+1, 1)], [P(idx, 2) P(idx+1, 2)], 'r');
end
plot([P(end, 1) P(1, 1)], [P(end, 2) P(1, 2)], 'r');
% Sample points within the triangle
N = 100000; % Number of points
t = sqrt(rand(N, 1));
s = rand(N, 1);
sample_pts = (1-t)*P(1, :)+bsxfun(#times, ((1-s)*P(2, :)+s*P(3, :)), t);
% Colors for demo
C = ones(size(sample_pts, 1), 1).*sample_pts(:, 1);
% Scatter sample points
scatter(sample_pts(:, 1), sample_pts(:, 2), [], C, 'filled');
colorbar;

MATLAB Simple Point Plots

In a while loop, I need to plot the positions (x,y) of two entities. That is, all I need to do is generate a plot with two points on it. I need to scale the plot to a specific maximum x and y value. An additional requirement is the fact that one of the points needs to have three concentric rings placed around it, each with a given radius. Additionally, this all is to happen in a loop, thus I'm hoping that only a single plot window opens and that I don't get a whole slew of windows opening (one for each loop iteration).
Basically here's the pseudo-code I'm trying (and failing!) to implement:
-> Open new plot window, with a given x and y axis
while (running) {
-> Clear the plot, so figure is nice and clean
-> Plot the two points
-> Plot the three circles around point A
}
I found several items in MATLAB's documentation, but no single plotting functions seems to do what I want, or there are instances where I inadvertently create multiple plots with only some of the data (i.e., one plot has the points and another has the circles).
here's a sample code you can use in your while loop
x0=1; y0=4;
x1=2; y1=3; % the x-y points
r=[1 2 3]; % 3 radii of concentrating rings
ang=0:0.01:2*pi;
xc=cos(ang)'*r;
yc=sin(ang)'*r;
plot(x0,y0,'.',x1,y1,'.'); % plot the point A
hold on
plot(x1+xc,y1+yc); % plot the 3 circles
% set the limits of the plots (though Matlab does it for you already)
xlim([min([x0 x1])-max(r) max([x0 x1])+max(r)]);
ylim([min([y0 y1])-max(r) max([y0 y1])+max(r)]);
hold off
you can make this work in a loop quite easily, read matlab's documentation on how to do that.
Try something like this:
r = [0.25 0.125 0.0625];
d = (1:360) / 180 * pi;
xy_circle = [cos(d)' sin(d)'];
xy_circle_1 = r(1) * xy_circle;
xy_circle_2 = r(2) * xy_circle;
xy_circle_3 = r(3) * xy_circle;
h_plot = plot(0, 0, '.k');
hold on
h_circle_1 = plot(xy_circle_1(:, 1), xy_circle_1(:, 2), '-b');
h_circle_2 = plot(xy_circle_2(:, 1), xy_circle_2(:, 2), '-r');
h_circle_3 = plot(xy_circle_3(:, 1), xy_circle_3(:, 2), '-g');
axis equal
for hh = 1:100
xy = rand(2, 2) / 4 + 0.375;
xlim = [0 1];
ylim = [0 1];
set(h_plot, 'XData', xy(:, 1));
set(h_plot, 'YData', xy(:, 2));
set(gca, 'XLim', xlim)
set(gca, 'YLim', ylim)
set(h_circle_1, 'XData', xy_circle_1(:, 1) + xy(1, 1));
set(h_circle_1, 'YData', xy_circle_1(:, 2) + xy(1, 2));
set(h_circle_2, 'XData', xy_circle_2(:, 1) + xy(1, 1));
set(h_circle_2, 'YData', xy_circle_2(:, 2) + xy(1, 2));
set(h_circle_3, 'XData', xy_circle_3(:, 1) + xy(1, 1));
set(h_circle_3, 'YData', xy_circle_3(:, 2) + xy(1, 2));
pause(1)
end
You can change the parameters as you wish.
You can use the following functions
figure; %creates a figure
hold on; %overlays points and circles
clf; %clear the figure
and use two types of markers (. and o) of various sizes for the points and circles
plot(x,y, 'b.', 'MarkerSize', 4);
plot(x,y, 'ro', 'MarkerSize', 10);
plot(x,y, 'go', 'MarkerSize', 14);
plot(x,y, 'bo', 'MarkerSize', 18);