MATLAB Answers

How to run a regression of lognrnd?

5 ビュー (過去 30 日間)
Fernanda Suarez Jaimes
Fernanda Suarez Jaimes 2020 年 3 月 12 日
I have a vector with random data log-norm distributed and I need to run a regression of this.

  5 件のコメント

表示 2 件の古いコメント
the cyclist
the cyclist 2020 年 3 月 12 日
You are using the word "regression", which suggests that you are trying to find the relationship between two variables. Usually, that would mean that you have
y = f(x) + error
and you would have the same number of elements in your x vector and your y vector.
But that doesn't seem right, in your example. Are you actually just trying to fit two different distributions (one a normal, and the other a lognormal)?
Fernanda Suarez Jaimes
Fernanda Suarez Jaimes 2020 年 3 月 12 日
Yes, correct. The thing is that I transformed the time_series1 into a new "weekly" time series with 6,000 elements as well. So I can fit the two distributions together. Like this
%Converting the 'daily' ts to a to a 'weekly' one
arrayts1 = padarray (time_series1, [ceil(numel (time_series1) / 7) * 7 - numel(time_series1) 0], 0, 'post' );
time_series3 = mean (reshape (arrayts1, 7, []), 1);
Then in the Y axis are the random valued and in the X axis are the time values.
I need to get a regression of X over Y.
This is what I have so far:
scatter(time_series2,time_series3,'.') % plot the data points
hAx=gca; hAx.XScale='log'; % on semilog x axis
b=polyfit(log(time_series2),Y,1); % fit response to log(independent) Y is the meanleast square value given by the fit for the above specific dataset
yHat=polyval(b,log([min(time_series2) max(time_series2)])) % remember, it's log in X
hold on
plot([min(time_series2) max(time_series2)],yHat,'r-')
Fernanda Suarez Jaimes
Fernanda Suarez Jaimes 2020 年 3 月 12 日
This is another option I have. The issue is that we need to do a regression of a time series. Therefore, the result of the model is not significant as it is near 0,05.
logarithms_ts2=log(time_series2); %calculating logarithm of the values in order to use GLM fuction
[regression2,dev,stats] = glmfit(xtime2,logarithms_ts2,'normal');
regression2=fitglm(xtime2,time_series2) %regression of time series 2


回答 (1 件)

Steven Lord
Steven Lord 2020 年 3 月 12 日
It sounds like you want the lognfit function.

  1 件のコメント

Fernanda Suarez Jaimes
Fernanda Suarez Jaimes 2020 年 3 月 12 日
You mean something as this?
rng ( 'default' ); % So that numbers can be repeated
time_series2 = lognrnd (0,0.25,6000,1); % generating time series with mu set to zero and sigma 0.25
[pHat,pCI] = lognfit(time_series2,0.01)
The result I get is this:
pHat =
0.0017 0.2494
pCI =
-0.0066 0.2437
0.0100 0.2554


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by