Calculating slope of a curve at a single point (where x = 0.5) and testing that slope for significance (slope ~= 0)

5 views (last 30 days)
Hi there,
I have been fitting a logistic regression model to some data using glmfit:
Data:
dataStims = [(16/36), (16/28), (16/16), (28/16), (36/16)]; % stimulus sizes
dataStims_long = linspace(dataStims(1), dataStims(end), 5000); % make long vector for stimulus sizes - to allow you to make a smoother curve when you fit a curve to your data
dataStims = log(dataStims); % take log to make spacing more even
dataStims_long = log(dataStims_long); % ''
dataResp = [9, 8, 6, 6, 0]; % count of affirmative responses (question: 'stimulus was down?' (options: down/ across)
dataCount = [10, 10, 10, 10, 10]; % count of trials presented
Code:
[~, ~, stats] = glmfit(dataStims', [dataResp' dataCount'], 'binomial', 'logit'); % fit logistic reg
This allows me to get some coefficients for the fit (stats.beta), which are
Output:
stats.beta =
0.4667
-2.1432
Which I understand to be the y-intercept and slope, respectively (someone please correct me if I am wrong). I also believe that the slope value returned is the slope of the line at the y-intercept (y = 0), is this correct too?
I am a bit confused re. the y-intercept, because it looks from the plot like the y-intercept ~= .6 (see image below). That is, the x value when y = 0 is ~= .6. But the y-intercept according to the stats.beta = 0.4667. Could someone let me know if I have misunderstood this?
FYI, I generated the curve using the following
Code:
logitFit_long = glmval(logitCoef, dataStims_long', 'logit');
To my main question: what I would like to do, is get the slope not at the y-intercept, but at the point of the line where x = .5 (marked by a * on the plot below). I want to get the slope at that point, and then test the significance of the slope.
Does anyone know a good way to do this?
I tried the below, which I think does what I intend by getting me the slope at the x = .05 point
Code:
[~, i] = min(abs(logitFit_long-.5)); % find value closest to x = .5
x = dataStims_long'; y = logitFit_long;
index=[i-1 i+1]; % takes the points where you want to calculate your slope (regression)
pse.coeff = polyfit(x(index),y(index),1); % calc coeff
pse.slope_line = polyval(pse.coeff,x); % calc line
Output:
pse.coeff =
-0.5358 0.6167
Which I understand to be the slope and the y-intercept, respectively. Correct?
This looks like what I want from plotting the pse.slope_line – see dashed line below. Looks like it is the slope at x = 0.5, as it intercepts the curve at the * point
What I would then like to do is to test whether this slope is significantly different from 0, i.e., is there a slope on that line or is it flat (slope = 0)?
With the glmfit stats from above, I am able to get the significance of the slope at the y-intercept using stats.t(2) and stats.p(2), for the t and p values of the significance test of the slope (the 2ndbeta value, i.e., the ‘2’).
But I am not sure how I would do it for the slope of the line at this x = .5 point. I tried a few things but they are giving me very odd values and I assume they are incorrect.
Does anyone have any ideas?
Thank you for your help,
Harriet
  3 Comments
HDJ
HDJ on 29 Jun 2022
Hi Torsten,
My apologies, I had pasted the code incorrectly. The negative values come from logging the data. I am not trying to log negative values, so no complex values.
I have amended my question as above

Sign in to comment.

Answers (1)

Nipun
Nipun on 5 Oct 2023
Hi HDJ,
I understand that you are trying to find the slope at a given point in a fitted plot. From my analysis, you have implemented a logistic regression to the set of given points.
There are two ways through which you can obtain the slope at a given point:
  1. Get access to the symbolic equation of the fitted plot. Differentiate the equation for first order around x=0.5 . You can yse the diff in-built functionality.
  2. Find the index of the x-axis point with value closest to 0.5 . Let the index be i and consider indices i-1, i+1. Fit a line through these points and that should, approximately, give you the derivative of the underlying equation at x=0.5
I understand that you have already tried approach 2. For approach 1, refer to the documentation for glmfit in order to retrieve the coefficients for the equation. The coefficients can be retrieved by using stats.beta field.
Link to the resources:
Hope this helps
Regards,
Nipun

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!