Add Regression Line Equation and R-Square to a PLOTNINE
Asked Answered
I

1

5

It is easy to get a linear best fit of data in plotnine --using stat_smooth(method="gls"). However, I can't figure out how to get out the coefficients to the best fit line or the R2 value. Ggplot in R has a stat_regline_equation() function that does this, but I cannot find a similar tool in plotnine.

Currently, I am using statsmodels.formula.api.ols to get these values, but there has to be a better way inside of plotnine.

PS: I'm a newbie to all things coding.

Ita answered 9/4, 2020 at 22:30 Comment(1)
stat_regline_equation() is not a ggplot function, but rather comes from ggpubr extension, so I would not expect it to be a part of plotnine. if you have a working substitute using statsmodels.formula.api.ols you may want to post it here to guide other users, and maybe then someone will be able to improve it for you.Athematic
I
6

I ended up using the following code; not PlotNine but very easy to implement.

import plotnine as p9
from scipy import stats
from plotnine.data import mtcars as df

#calculate best fit line
slope, intercept, r_value, p_value, std_err = stats.linregress(df['wt'],df['mpg'])
df['fit']=df.wt*slope+intercept
#format text 
txt= 'y = {:4.2e} x + {:4.2E};   R^2= {:2.2f}'.format(slope, intercept, r_value*r_value)
#create plot. The 'factor' is a nice trick to force a discrete color scale
plot=(p9.ggplot(data=df, mapping= p9.aes('wt','mpg', color = 'factor(gear)'))
    + p9.geom_point(p9.aes())
    + p9.xlab('Wt')+ p9.ylab(r'MPG')
    + p9.geom_line(p9.aes(x='wt', y='fit'), color='black')
    + p9.annotate('text', x= 3, y = 35, label = txt))
#for some reason, I have to print my plot 
print(plot)
Ita answered 13/4, 2020 at 19:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.