Fitted regression line parameters in python
Question:
How can I get the intercept and the slope of the fitted regression line in qqplot
? Here’s a small working example, where I want the parameters of the red regression line:
import statsmodels.api as sm
import scipy.stats as stats
import numpy as np
np.random.seed(100)
a = np.random.normal(0, 4, 100)
sm.qqplot(a, stats.norm(loc=5, scale=1), line="r")
Answers:
You can extract the x,y coordinates of the line and use numpy polyfit to get your slope and intercept.
import statsmodels.api as sm
import scipy.stats as stats
import numpy as np
np.random.seed(100)
a = np.random.normal(0, 4, 100)
fig = sm.qqplot(a, stats.norm(loc=5, scale=1), line="r")
line = fig.gca().lines[-1]
slope, intercept = np.polyfit(line.get_xdata(),line.get_ydata(),1)
print(slope, intercept)
Output
4.027047644266026 -20.55190163670639
Use the returned figure to grab the relevant axes and line and calculate slope and intercept from the xydata. For reference, fig.axes[0].lines[0] is the data, lines[1] is the fit.
import statsmodels.api as sm
import scipy.stats as stats
import numpy as np
np.random.seed(100)
a = np.random.normal(0, 4, 100)
fig = sm.qqplot(a, stats.norm(loc=5, scale=1), line="r")
# Grab line data from figure
xydata = fig.axes[0].lines[1].get_xydata()
diff = (xydata - np.roll(xydata, 1, axis = 0))[1:]
slopes = diff[:, 1]/diff[:, 0]
slope = slopes.mean()
y_ints = xydata[:, 1] - slope*xydata[:, 0]
y_int = y_ints.mean()
# Re-plot line for visual confirmation
plt.plot(xydata[:, 0], xydata[:, 0]*slope + y_int, 'k--')
How can I get the intercept and the slope of the fitted regression line in qqplot
? Here’s a small working example, where I want the parameters of the red regression line:
import statsmodels.api as sm
import scipy.stats as stats
import numpy as np
np.random.seed(100)
a = np.random.normal(0, 4, 100)
sm.qqplot(a, stats.norm(loc=5, scale=1), line="r")
You can extract the x,y coordinates of the line and use numpy polyfit to get your slope and intercept.
import statsmodels.api as sm
import scipy.stats as stats
import numpy as np
np.random.seed(100)
a = np.random.normal(0, 4, 100)
fig = sm.qqplot(a, stats.norm(loc=5, scale=1), line="r")
line = fig.gca().lines[-1]
slope, intercept = np.polyfit(line.get_xdata(),line.get_ydata(),1)
print(slope, intercept)
Output
4.027047644266026 -20.55190163670639
Use the returned figure to grab the relevant axes and line and calculate slope and intercept from the xydata. For reference, fig.axes[0].lines[0] is the data, lines[1] is the fit.
import statsmodels.api as sm
import scipy.stats as stats
import numpy as np
np.random.seed(100)
a = np.random.normal(0, 4, 100)
fig = sm.qqplot(a, stats.norm(loc=5, scale=1), line="r")
# Grab line data from figure
xydata = fig.axes[0].lines[1].get_xydata()
diff = (xydata - np.roll(xydata, 1, axis = 0))[1:]
slopes = diff[:, 1]/diff[:, 0]
slope = slopes.mean()
y_ints = xydata[:, 1] - slope*xydata[:, 0]
y_int = y_ints.mean()
# Re-plot line for visual confirmation
plt.plot(xydata[:, 0], xydata[:, 0]*slope + y_int, 'k--')