News

New paper! in the American Naturalist

Friday, January 31, 2020

Linear regression with matplotlib and numpy

import matplotlib.pyplot as plt
import numpy as np

x # x axis data (list)
y # y axis data (list)
xp = np.linspace(min(x),max(x),100) # points on x axis for the fitted line
z = np.polyfit(x,y,1) # the third argument is to specify the dimension in the polynomial fitting.
p = np.poly1d(z) # p(xp) gives the function of the regression for values in xp
plt.plot(x, y, '.', xp, p(xp), '-')


See here about how to compute p-value of the coefficients (and F-statistics for multiple regressions)!