0

I have data that y and x don't have a linear trend. The data as follows and if you plot y as a function of x, the plot is nonlinear.

x= [45.5976, 45.6311, 45.6599, 45.679, 45.703, 45.7461, 45.7749]
y = [0.17, 1.7, 5.1, 17, 51, 170, 510]
plot(x,y,'o')

My goal is to find an optimum value of b to make log(y) behavior with respect to log((x-b)/b) a linear relation. In other words, plot(log((x-b)/b),log(y) should produce a linear function.

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
Nini
  • 23
  • 1
  • 6
  • 1
    How do you define an optimal value of `b`? Optimal with respect to what criteria? Is `plot(x,log(y),'o')` not linear enough? – SecretAgentMan Mar 09 '20 at 16:37
  • @SecretAgentMan the goal is to find a suitable value of "b" to make the relation between ln(y) respect ln((x-b)/b) a linear relation. – Nini Mar 09 '20 at 16:40
  • 1
    Ty, I got that part. I was curious if you had a quantitative measure to "optimize" before I posted my answer. There are an infinite number of choices for `b` that will work. I think a suitable feasible solution is possible. – SecretAgentMan Mar 09 '20 at 16:41
  • @SecretAgentMan the plot(x,log(y),'o') is linear but I want to linearize this graph: plot(log(y),log((x-b)/b) – Nini Mar 09 '20 at 16:43
  • 1
    Did you mean `plot(log(y),log((x-b)/b)` or `plot(log((x-b)/b),log(y))` ? – SecretAgentMan Mar 09 '20 at 16:50
  • 1
    I've plotted both functions using different `b` values ranging from 0.5 to 2 and get seemingly similar linear plots. Have you tried this? Could you please [edit] your question to clarify my previous comment? – SecretAgentMan Mar 09 '20 at 16:56

1 Answers1

2

Since I don't have enough reputation to add a comment to clarify the question, I'm trying to help in an answer. Also, typically when transforming data to fit a linear regression, if your original model is: y = b0 + b1x, then taking logs of both the predictor and response gives a new model y* = b0 + b1x* where y* = ln(y) and x* = ln(x). Why did you decide your model should be of the form: ln(y) = ln((x-b)/b)?

In any case, to find the optimal beta values for such a model in Matlab you would do something like the following:

x= [45.5976, 45.6311, 45.6599, 45.679, 45.703, 45.7461, 45.7749]';
y = [0.17, 1.7, 5.1, 17, 51, 170, 510]';
figure(1), plot(x,y,'o');

ln_y = log(y);
ln_x = log(x);
figure(2), plot(ln_x, ln_y, 'x');

ln_X = [ones(length(ln_x),1) ln_x];
B = ln_X\ln_y;

ln_y_fitted = ln_X*B;
figure(2), 
hold on
plot(ln_x, ln_y_fitted, '--', 'Color', 'r');

Given the above code, if you want to plot the various results for log(y) = log((x-b)/b), you can use something like this:

for b = 0.1:0.1:4
    ln_x = log((x-b)/b);
    figure, plot(ln_x, ln_y, 'x');
end
lincolnck
  • 302
  • 1
  • 12
  • @lincolnk, thanks for your help. Your code creates a linear relation between ln_x and ln_y_fitted but my goal is to find the value of "b" which makes the plot of plot(log(y),log((x-b)/b) close to a linear line trend. – Nini Mar 09 '20 at 16:48
  • I agree with the approach. However, note that the [OP specifically requested](https://stackoverflow.com/questions/60604069/need-help-to-linearize-the-data#comment107220440_60604069) to linearize this particular relationship: `plot(log(y),log((x-b)/b)`. – SecretAgentMan Mar 09 '20 at 16:48
  • I see. @Nini, I agree with @SecretAgentMan. In the case that you want to find the optimal `b` such that `log(y) = log((x-b)/b)` is the most linear, you need to define some sort of quantitative measure to minimize/maximize. – lincolnck Mar 09 '20 at 17:13
  • 1
    Per [my comment](https://stackoverflow.com/questions/60604069/how-to-linearize-this-data-for-a-specific-relationship-in-matlab#comment107220844_60604069), your edit to show the plot for various values of `b` reveals many feasible solutions. (+1) Further detail needed from the OP to provide improved method. – SecretAgentMan Mar 09 '20 at 17:31
  • @lincolnck would it be possible to have a constraint on b? for example to limit the search engine to min and max values such as min < b < max? – Nini Mar 09 '20 at 18:21
  • @Nini [this](https://stackoverflow.com/questions/60604069/how-to-linearize-this-data-for-a-specific-relationship-in-matlab#comment107223105_60604964) begins to alter the question beyond the original scope. Please take care not to invalidate existing answers. If the question requires too much changing, it might be best to ask a new question. In either event, please [edit] your question to include all clarifications so they aren't buried in the comments. This will help future visitors. – SecretAgentMan Mar 09 '20 at 18:48