Building a simple program for statistics
In the first section, you already measure room temperature. Now we will try to perform some simple computational statistics using Statsmodels. We will use our measurement results data and then build a linear regression for our data.
First, we should install Statsmodels. This library needs required libraries such as NumPy, SciPy, pandas, and patsy. We can install them using pip
. Type the following command:
$ pip install numpy scipy pandas patsy statsmodels
If you get a problem related to security access, you can run this command using sudo
:
$ sudo pip install numpy scipy pandas patsy statsmodels
If your computer doesn't have pip
installed, you can install it by following the guidelines at https://pip.pypa.io/en/stable/installing/.
For testing, we create a Python program. Write the following scripts:
import numpy as np import statsmodels.api as sm # room temperature Y = [18, 17, 18, 19, 20, 20, 21, 22, 22, 24, 25, 26, 28, 29, 28, 27, 25, 24, 24, 23, 22, 20, 19, 19] X = range(1, 25) X = sm.add_constant(X) model = sm.OLS(Y, X) results = model.fit() # print print(results.params) print(results.tvalues) print(results.t_test([1, 0])) print(results.f_test(np.identity(2)))
We build a linear regression using sm.OLS()
. We then do estimation using model.fit()
. Finally, we print the computation result. Save the program in a file called ch01_linear.py
.
Now you can run this program using the following command:
$ python ch01_linear.py
If you have installed Python 3, you can run this program using the following command:
$ python3 ch01_linear.py
You should see the program output shown in the following screenshot. I run this program using Python 3: