Skip to content

Commit

Permalink
ols -> smf.ols : 1
Browse files Browse the repository at this point in the history
  • Loading branch information
spring-haru committed Jul 21, 2024
1 parent 648ddd2 commit c06e5ba
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 69 deletions.
22 changes: 11 additions & 11 deletions 10_Residuals.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
"import numpy as np\n",
"import pandas as pd\n",
"import statsmodels.api as sm\n",
"import statsmodels.formula.api as smf\n",
"\n",
"from statsmodels.formula.api import ols\n",
"from scipy.stats import norm, uniform\n",
"\n",
"# 警告メッセージを非表示\n",
Expand Down Expand Up @@ -131,7 +131,7 @@
" \n",
" df = pd.DataFrame({'Y':y, 'X':x}) # DataFrame\n",
" \n",
" res = ols(formula='Y ~ X', data=df).fit() # OLSの計算\n",
" res = smf.ols(formula='Y ~ X', data=df).fit() # OLSの計算\n",
" u_standardized = res.get_influence().resid_studentized_internal # 標準化残差\n",
" \n",
" return x, y, res.fittedvalues, res.resid, u_standardized, res.rsquared # 返り値の設定"
Expand Down Expand Up @@ -596,7 +596,7 @@
"\n",
"df_diag = pd.DataFrame({'Y':y, 'X':x}) # DataFrameの作成\n",
"\n",
"res_diag = ols(formula='Y ~ X', data=df_diag).fit() # OLS推定"
"res_diag = smf.ols(formula='Y ~ X', data=df_diag).fit() # OLS推定"
]
},
{
Expand Down Expand Up @@ -914,7 +914,7 @@
"\n",
"df = pd.DataFrame({'Y':y, 'X':x}) # DataFrame\n",
"\n",
"res = ols(formula='Y ~ X', data=df).fit() # OLSの計算\n",
"res = smf.ols(formula='Y ~ X', data=df).fit() # OLSの計算\n",
"resid_std = res.get_influence().resid_studentized_internal # 標準化残差\n",
"\n",
"plt.scatter(res.fittedvalues,resid_std) # 散布図\n",
Expand Down Expand Up @@ -991,7 +991,7 @@
"y = 1.0 + 0.1*x +0.1*x2+ u\n",
"df = pd.DataFrame({'Y':y, 'X':x})\n",
"\n",
"res = ols(formula='Y ~ X', data=df).fit()\n",
"res = smf.ols(formula='Y ~ X', data=df).fit()\n",
"resid_std = res.get_influence().resid_studentized_internal\n",
"\n",
"plt.scatter(res.fittedvalues,resid_std)\n",
Expand Down Expand Up @@ -1078,7 +1078,7 @@
"\n",
"df = pd.DataFrame({'Y':y, 'X':x})\n",
"\n",
"res = ols(formula='Y ~ X', data=df).fit()\n",
"res = smf.ols(formula='Y ~ X', data=df).fit()\n",
"resid_std = res.get_influence().resid_studentized_internal\n",
"\n",
"plt.scatter(res.fittedvalues,resid_std)\n",
Expand Down Expand Up @@ -1160,7 +1160,7 @@
"y = 1 + 0.1*x + x**0.6*u\n",
"df = pd.DataFrame({'Y':y, 'X':x})\n",
"\n",
"res = ols(formula='Y ~ X', data=df).fit()\n",
"res = smf.ols(formula='Y ~ X', data=df).fit()\n",
"resid_std = res.get_influence().resid_studentized_internal\n",
"\n",
"plt.scatter(res.fittedvalues,resid_std)\n",
Expand Down Expand Up @@ -1232,10 +1232,10 @@
"df_cd = pd.DataFrame({'Y':y, 'X':x})\n",
"\n",
"# 外れ値がない場合のOLS\n",
"res_no = ols(formula='Y ~ X', data=df_cd.loc[0:19,:]).fit()\n",
"res_no = smf.ols(formula='Y ~ X', data=df_cd.loc[0:19,:]).fit()\n",
"\n",
"# 外れ値がある場合のOLS\n",
"res_cd = ols(formula='Y ~ X', data=df_cd).fit()"
"res_cd = smf.ols(formula='Y ~ X', data=df_cd).fit()"
]
},
{
Expand Down Expand Up @@ -1348,7 +1348,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.11.9"
},
"nteract": {
"version": "0.23.1"
Expand All @@ -1368,5 +1368,5 @@
}
},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 4
}
8 changes: 4 additions & 4 deletions 11_Inference.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import pandas as pd\n",
"import statsmodels.formula.api as smf\n",
"import wooldridge\n",
"\n",
"from statsmodels.formula.api import ols\n",
"from scipy.stats import t, f\n",
"\n",
"# 警告メッセージを非表示\n",
Expand Down Expand Up @@ -505,7 +505,7 @@
"outputs": [],
"source": [
"formula_gpa = 'colGPA ~ hsGPA + ACT + skipped'\n",
"res_gpa = ols(formula_gpa, data=gpa).fit()"
"res_gpa = smf.ols(formula_gpa, data=gpa).fit()"
]
},
{
Expand Down Expand Up @@ -988,7 +988,7 @@
"outputs": [],
"source": [
"formula_0 = 'np.log(salary) ~ years + gamesyr + bavg + hrunsyr + rbisyr'\n",
"res_0 = ols(formula_0, data=mlb1).fit()"
"res_0 = smf.ols(formula_0, data=mlb1).fit()"
]
},
{
Expand Down Expand Up @@ -1033,7 +1033,7 @@
"outputs": [],
"source": [
"formula_1 = 'np.log(salary) ~ years + gamesyr'\n",
"res_1 = ols(formula_1, data=mlb1).fit()"
"res_1 = smf.ols(formula_1, data=mlb1).fit()"
]
},
{
Expand Down
14 changes: 7 additions & 7 deletions 12_Asymptotics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import pandas as pd\n",
"import statsmodels.formula.api as smf\n",
"import wooldridge\n",
"\n",
"from scipy.stats import gaussian_kde, t\n",
"from statsmodels.formula.api import ols\n",
"from statsmodels.api import qqplot\n",
"from statsmodels.stats.stattools import jarque_bera, omni_normtest\n",
"from numba import njit\n",
Expand Down Expand Up @@ -1236,7 +1236,7 @@
"source": [
"wage1 = wooldridge.data('wage1')\n",
"formula_wage = 'wage ~ educ + exper+ tenure'\n",
"res_wage = ols(formula_wage, data=wage1).fit()\n",
"res_wage = smf.ols(formula_wage, data=wage1).fit()\n",
"qqplot(res_wage.resid, line='45',fit=True)\n",
"pass"
]
Expand All @@ -1260,7 +1260,7 @@
"source": [
"wage1 = wooldridge.data('wage1')\n",
"formula_wage_log = 'np.log(wage) ~ educ + exper+ tenure'\n",
"res_wage_log = ols(formula_wage_log, data=wage1).fit()\n",
"res_wage_log = smf.ols(formula_wage_log, data=wage1).fit()\n",
"qqplot(res_wage_log.resid, line='45',fit=True)\n",
"pass"
]
Expand Down Expand Up @@ -1613,7 +1613,7 @@
"outputs": [],
"source": [
"form_0 = 'narr86 ~ pcnv + ptime86 + qemp86 + avgsen + tottime'\n",
"res_0 = ols(form_0, data=crime1).fit()\n",
"res_0 = smf.ols(form_0, data=crime1).fit()\n",
"res_0.params"
]
},
Expand Down Expand Up @@ -1646,7 +1646,7 @@
"outputs": [],
"source": [
"form_1 = 'narr86 ~ pcnv + ptime86 + qemp86'\n",
"res_1 = ols(form_1, data=crime1).fit()\n",
"res_1 = smf.ols(form_1, data=crime1).fit()\n",
"res_1.params"
]
},
Expand Down Expand Up @@ -1710,7 +1710,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.11.9"
},
"nteract": {
"version": "0.15.0"
Expand All @@ -1730,5 +1730,5 @@
}
},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 4
}
26 changes: 13 additions & 13 deletions 13_Dummies.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"import statsmodels.formula.api as smf\n",
"import wooldridge\n",
"from statsmodels.formula.api import ols\n",
"\n",
"# 警告メッセージを非表示\n",
"import warnings\n",
Expand Down Expand Up @@ -114,7 +114,7 @@
"source": [
"form_const = 'wage ~ 1' # 定数項だけの場合は1が必要\n",
"\n",
"res_const = ols(form_const, data=wage1).fit()\n",
"res_const = smf.ols(form_const, data=wage1).fit()\n",
"\n",
"res_const.params"
]
Expand Down Expand Up @@ -213,7 +213,7 @@
"source": [
"form_const_2 = 'wage ~ female'\n",
"\n",
"res_const_2 = ols(form_const_2, data=wage1).fit()\n",
"res_const_2 = smf.ols(form_const_2, data=wage1).fit()\n",
"\n",
"res_const_2.params"
]
Expand Down Expand Up @@ -481,7 +481,7 @@
"source": [
"form_const_4 = 'wage ~ marmale + singfem + marfem'\n",
"\n",
"res_const_4 = ols(form_const_4, data=wage1).fit()\n",
"res_const_4 = smf.ols(form_const_4, data=wage1).fit()\n",
"\n",
"para4 = res_const_4.params\n",
"para4"
Expand Down Expand Up @@ -631,7 +631,7 @@
"source": [
"form_1 = 'wage ~ female + educ + exper+ tenure'\n",
"\n",
"res_1 = ols(form_1, data=wage1).fit()\n",
"res_1 = smf.ols(form_1, data=wage1).fit()\n",
"\n",
"res_1.params"
]
Expand Down Expand Up @@ -712,7 +712,7 @@
"source": [
"form_2 = 'np.log(wage) ~ female + female:educ + educ + exper + tenure'\n",
"\n",
"res_2 = ols(form_2, data=wage1).fit()"
"res_2 = smf.ols(form_2, data=wage1).fit()"
]
},
{
Expand Down Expand Up @@ -891,7 +891,7 @@
"source": [
"form_c = 'wage ~ C(sex) + educ'\n",
"\n",
"res_c = ols(form_c, data=df).fit()\n",
"res_c = smf.ols(form_c, data=df).fit()\n",
"\n",
"res_c.params"
]
Expand Down Expand Up @@ -936,7 +936,7 @@
"source": [
"form_cm = 'wage ~ C(sex,Treatment(\"male\")) + educ'\n",
"\n",
"res_cm = ols(form_cm, data=df).fit()\n",
"res_cm = smf.ols(form_cm, data=df).fit()\n",
"\n",
"res_cm.params"
]
Expand All @@ -960,7 +960,7 @@
"source": [
"form_ca = 'wage ~ female + educ'\n",
"\n",
"res_ca = ols(form_ca, data=df).fit()\n",
"res_ca = smf.ols(form_ca, data=df).fit()\n",
"\n",
"res_ca.params"
]
Expand Down Expand Up @@ -1046,7 +1046,7 @@
"source": [
"form_p = 'np.log(pfries) ~ prpblck + np.log(income) + C(chain)'\n",
"\n",
"res_p = ols(form_p, data=discrim).fit()\n",
"res_p = smf.ols(form_p, data=discrim).fit()\n",
"\n",
"print(res_p.summary().tables[1])"
]
Expand Down Expand Up @@ -1202,7 +1202,7 @@
"source": [
"form_c = 'np.log(pfries) ~ prpblck + np.log(income) + chain'\n",
"\n",
"res_c = ols(form_c, data=df_c).fit()\n",
"res_c = smf.ols(form_c, data=df_c).fit()\n",
"\n",
"print(res_c.summary().tables[1])"
]
Expand Down Expand Up @@ -1237,7 +1237,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.11.9"
},
"nteract": {
"version": "0.15.0"
Expand All @@ -1257,5 +1257,5 @@
}
},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 4
}
14 changes: 7 additions & 7 deletions 14_Hetero.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
"import lmdiag\n",
"import numpy as np\n",
"import pandas as pd\n",
"import statsmodels.formula.api as smf\n",
"import wooldridge\n",
"\n",
"from seaborn import residplot\n",
"from statsmodels.formula.api import ols\n",
"from statsmodels.stats.api import het_breuschpagan, het_white\n",
"from statsmodels.stats.outliers_influence import reset_ramsey\n",
"\n",
Expand Down Expand Up @@ -215,7 +215,7 @@
"source": [
"form_ols = 'cumgpa ~ sat + hsperc + tothrs + female + black + white'\n",
"\n",
"mod_ols = ols(form_ols, data=gpa3)\n",
"mod_ols = smf.ols(form_ols, data=gpa3)\n",
"res_ols = mod_ols.fit()\n",
"\n",
"print(res_ols.summary().tables[1])"
Expand Down Expand Up @@ -328,7 +328,7 @@
},
"outputs": [],
"source": [
"res_HC3 = ols(form_ols, data=gpa3).fit(cov_type='HC3', use_t=True)\n",
"res_HC3 = smf.ols(form_ols, data=gpa3).fit(cov_type='HC3', use_t=True)\n",
"\n",
"print(res_HC3.summary().tables[1])"
]
Expand Down Expand Up @@ -522,7 +522,7 @@
"source": [
"form_h = 'price ~ lotsize + sqrft + bdrms'\n",
"\n",
"res_h = ols(form_h, data=hprice1).fit()\n",
"res_h = smf.ols(form_h, data=hprice1).fit()\n",
"\n",
"print(res_h.summary().tables[1])"
]
Expand Down Expand Up @@ -595,7 +595,7 @@
"source": [
"form_h_log = 'np.log(price) ~ np.log(lotsize) + np.log(sqrft) + bdrms'\n",
"\n",
"res_h_log = ols(form_h_log, data=hprice1).fit()\n",
"res_h_log = smf.ols(form_h_log, data=hprice1).fit()\n",
"\n",
"print(res_h_log.summary().tables[1])"
]
Expand Down Expand Up @@ -1090,7 +1090,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.11.9"
},
"nteract": {
"version": "0.15.0"
Expand All @@ -1110,5 +1110,5 @@
}
},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 4
}
Loading

0 comments on commit c06e5ba

Please sign in to comment.