{"id":5153,"date":"2022-11-28T14:41:30","date_gmt":"2022-11-28T06:41:30","guid":{"rendered":"https:\/\/seit2019.xyz\/?p=5153"},"modified":"2022-11-28T15:01:52","modified_gmt":"2022-11-28T07:01:52","slug":"5-6-%e8%ae%a1%e9%87%8f%e5%88%86%e6%9e%906%ef%bc%9a%e5%bc%82%e6%96%b9%e5%b7%ae%e7%9a%84%e8%af%8a%e6%96%ad%e5%92%8c%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/seit2019.xyz\/?p=5153","title":{"rendered":"5.6 \u8ba1\u91cf\u5206\u67906\uff1a\u5f02\u65b9\u5dee\u7684\u8bca\u65ad\u548c\u5904\u7406"},"content":{"rendered":"<h3>\u4f8b\u5b50<\/h3>\n<p>\u4ee5\u8bfe\u672c\u886811-5\uff08\u7b2c396\u9875\uff09\u4e2d\u7684\u6570\u636e\u4e3a\u6837\u672c\uff0c\u5efa\u7acb\u5982\u4e0b\u56de\u5f52\u6a21\u578b\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/i0.wp.com\/seit2019.xyz\/wp-content\/uploads\/2020\/05\/%E5%AE%9E%E9%AA%8C4-1.png?w=750&amp;ssl=1\" alt=\"img\" \/><\/p>\n<h3>\u4ee3\u7801\u90e8\u5206<\/h3>\n<pre><code># lec06\n# \u8ba1\u91cf\u5b9e\u9a8c4-\u4ee3\u7801\u90e8\u5206 \u5f02\u65b9\u5dee\u7684\u8bca\u65ad\u4e0e\u5904\u7406\n\n# \u5bfc\u5165\u7a0b\u5e8f\u5305\nimport pandas as pd\nimport statsmodels.api as sm\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom statsmodels.stats.stattools import jarque_bera\nimport ssl\n\n# \u7981\u7528SSL\u8bc1\u4e66\u6821\u9a8c\nssl._create_default_https_context = ssl._create_unverified_context\n\n# \u5bfc\u5165(\u5728\u7ebf)\u6570\u636e\ndf = pd.read_excel(r&quot;https:\/\/cdn.seit2019.xyz\/data\/econometrics\/table11.5.xlsx&quot;, index_col=&quot;Industry&quot;)\n\n# \u5c55\u793a\u6570\u636e\nprint(&quot;\u6570\u636e\u5c55\u793a\u5982\u4e0b\uff1a&quot;)\nprint(df, &quot;\\n&quot;)\n\n# \u7b80\u8981\u7edf\u8ba1\u63cf\u8ff0\nprint(&quot;\u7b80\u8981\u7edf\u8ba1\u63cf\u8ff0\uff1a&quot;)\nprint(df.describe(), &quot;\\n&quot;)\n\n# OLS\u56de\u5f52\n# \u5b9a\u4e49\u53d8\u91cf\ny = df[&quot;RD&quot;]\nx = df[&quot;Sales&quot;]\n# \u6dfb\u52a0\u5e38\u6570\u9879\nX = sm.add_constant(x)\n\n# OLS\u56de\u5f52\nOLS_model = sm.OLS(y, X).fit()\nprint(&quot;OLS\u56de\u5f52\u7ed3\u679c\uff1a&quot;)\nprint(OLS_model.summary(), &quot;\\n&quot;)\n\n# \u751f\u6210\u6b8b\u5dee\u5e8f\u5217\ndf[&quot;e&quot;] = OLS_model.resid\ndf[&quot;e^2&quot;] = df[&quot;e&quot;]**2\ndf[&quot;Log(e^2)&quot;] = df[&quot;e^2&quot;].apply(np.log)\ndf[&quot;Abs(e)&quot;] = df[&quot;e&quot;].abs()\n\n# \u6b8b\u5dee\u6b63\u6001\u6027\ndf_jb = pd.DataFrame()\ndf_jb[&quot;Stats&quot;] = [&quot;Jarque_Bera&quot;, &quot;P_value&quot;, &quot;Skew&quot;, &quot;Kurt&quot;]\ndf_jb[&quot;Value&quot;] = jarque_bera(OLS_model.resid)\ndf_jb[&quot;Value&quot;] = df_jb[&quot;Value&quot;].round(4)\nprint(&quot;\u6b8b\u5dee\u6b63\u6001\u6027\uff1a&quot;)\nprint(df_jb.to_string(index=False), &quot;\\n&quot;)\n# \u7ed8\u56fe\uff1a\u8bbe\u7f6e\u7ed8\u56fe\u683c\u5f0f,\u652f\u6301\u6c49\u5b57\nplt.rcParams[&#039;font.sans-serif&#039;] = [&#039;SimHei&#039;]\nplt.rcParams[&#039;axes.unicode_minus&#039;] = False\n# \u7ed8\u56fe\uff1a\u6b8b\u5dee\u76f4\u65b9\u56fe\nplt.hist(OLS_model.resid, bins=7)\nplt.title(&quot;\u6b8b\u5dee\u76f4\u65b9\u56fe&quot;, fontsize=&quot;xx-large&quot;, fontweight=&quot;bold&quot;)\nplt.show()\n\n# \u7ed8\u56fe\uff1a\u6b8b\u5dee\u6563\u70b9\u56fe\n# \u7ed8\u56fe\uff1a\u8bbe\u7f6e\u7ed8\u56fe\u683c\u5f0f,\u652f\u6301\u6c49\u5b57\nplt.rcParams[&#039;font.sans-serif&#039;] = [&#039;SimHei&#039;]\nplt.rcParams[&#039;axes.unicode_minus&#039;] = False\n# \u7ed8\u56fe\uff1a\u5b9a\u4e49\u53d8\u91cf\nx = df.index\ny = df[&quot;e&quot;]\n# \u7ed8\u56fe\uff1a\u6b8b\u5dee\u6563\u70b9\u56fe\nplt.scatter(x, y, c=&quot;blue&quot;)\nplt.title(&quot;\u6b8b\u5dee\u6563\u70b9\u56fe&quot;, fontsize=&quot;xx-large&quot;, fontweight=&quot;bold&quot;)\nplt.xlabel(&quot;Industry&quot;, fontsize=&quot;xx-large&quot;, fontweight=&quot;bold&quot;)\nplt.ylabel(&quot;e&quot;, fontsize=&quot;xx-large&quot;, fontweight=&quot;bold&quot;)\nplt.xticks([])\nplt.show()\n\n# \u7ed8\u56fe\uff1a\u6b8b\u5dee\u4e0e\u89e3\u91ca\u53d8\u91cf\u7684\u6563\u70b9\u56fe\n# \u7ed8\u56fe\uff1a\u5b9a\u4e49\u53d8\u91cf\nx = df[&quot;Sales&quot;]\ny = df[&quot;e&quot;]\n# \u7ed8\u56fe\uff1a\u6b8b\u5dee\u6563\u70b9\u56fe\nplt.scatter(x, y, c=&quot;blue&quot;)\nplt.title(&quot;\u6b8b\u5dee\u4e0e\u89e3\u91ca\u53d8\u91cf\u7684\u6563\u70b9\u56fe&quot;, fontsize=&quot;xx-large&quot;, fontweight=&quot;bold&quot;)\nplt.xlabel(&quot;Sales&quot;, fontsize=&quot;xx-large&quot;, fontweight=&quot;bold&quot;)\nplt.ylabel(&quot;e&quot;, fontsize=&quot;xx-large&quot;, fontweight=&quot;bold&quot;)\nplt.show()\n\n# \u8bbe\u7f6e\u68c0\u9a8c\u7ed3\u679c\u6c47\u603b\u8868\ndf_test = pd.DataFrame(columns=[&quot;Test_Name&quot;, &quot;F_stat&quot;, &quot;P_value&quot;, &quot;Sig&quot;])\ndf_test[&quot;Test_Name&quot;] = [&quot;Park test&quot;, &quot;Glejser Test&quot;, &quot;White Test&quot;, &quot;B-P-G Test&quot;]\n\n# \u5f02\u65b9\u5dee\u8bca\u65ad\n\n# \u5e15\u514b\u68c0\u9a8c(Park test)\n# \u5b9a\u4e49\u53d8\u91cf\ny1 = df[&quot;Log(e^2)&quot;]\ndf[&quot;Log(Sales)&quot;] = df[&quot;Sales&quot;].apply(np.log)\nx1 = df[&quot;Log(Sales)&quot;]\n# \u8f85\u52a9\u56de\u5f52\nX1 = sm.add_constant(x1)\nOLS_aux1 = sm.OLS(y1, X1).fit()\nprint(&quot;\u8f85\u52a9\u56de\u5f521\u7ed3\u679c\uff1a&quot;)\nprint(OLS_aux1.summary(), &quot;\\n&quot;)\ndf_test.iloc[0, 1] = OLS_aux1.fvalue.round(4)\ndf_test.iloc[0, 2] = OLS_aux1.f_pvalue.round(4)\n# F\u7edf\u8ba1\u91cf\u663e\u8457\u6027\nif OLS_aux1.f_pvalue &lt; 0.05:\n    df_test.iloc[0, 3] = &quot;\u663e\u8457&quot;\n    print(&quot;\u7ed3\u8bba\uff1a\u5e15\u514b\u68c0\u9a8c(Park test)\u7684F\u7edf\u8ba1\u91cf\u663e\u8457\uff0cOLS\u56de\u5f52\u6a21\u578b\u4e2d\u5b58\u5728\u5f02\u65b9\u5dee\u3002&quot;)\nelse:\n    df_test.iloc[0, 3] = &quot;\u4e0d\u663e\u8457&quot;\n    print(&quot;\u7ed3\u8bba\uff1a\u5e15\u514b\u68c0\u9a8c(Park test)\u7684F\u7edf\u8ba1\u91cf\u4e0d\u663e\u8457\uff0cOLS\u56de\u5f52\u6a21\u578b\u4e2d\u4e0d\u5b58\u5728\u5f02\u65b9\u5dee\u3002&quot;)\n\n# \u683c\u83b1\u6cfd\u68c0\u9a8c(Glejser Test)\n# \u5b9a\u4e49\u53d8\u91cf\ny2 = df[&quot;Abs(e)&quot;]\nx2 = df[&quot;Sales&quot;]\n# \u6dfb\u52a0\u5e38\u6570\u9879\nX2 = sm.add_constant(x2)\n# \u8f85\u52a9\u56de\u5f52\nOLS_aux2 = sm.OLS(y2, X2).fit()\nprint(&quot;\u8f85\u52a9\u56de\u5f522\u7ed3\u679c\uff1a&quot;)\nprint(OLS_aux2.summary(), &quot;\\n&quot;)\ndf_test.iloc[1, 1] = OLS_aux2.fvalue.round(4)\ndf_test.iloc[1, 2] = OLS_aux2.f_pvalue.round(4)\n# F\u7edf\u8ba1\u91cf\u663e\u8457\u6027\nif OLS_aux2.f_pvalue &lt; 0.05:\n    df_test.iloc[1, 3] = &quot;\u663e\u8457&quot;\n    print(&quot;\u7ed3\u8bba\uff1a\u683c\u83b1\u6cfd\u68c0\u9a8c(Glejser Test)\u7684F\u7edf\u8ba1\u91cf\u663e\u8457\uff0cOLS\u56de\u5f52\u6a21\u578b\u4e2d\u5b58\u5728\u5f02\u65b9\u5dee\u3002&quot;)\nelse:\n    df_test.iloc[1, 3] = &quot;\u4e0d\u663e\u8457&quot;\n    print(&quot;\u7ed3\u8bba\uff1a\u683c\u83b1\u6cfd\u68c0\u9a8c(Glejser Test)\u7684F\u7edf\u8ba1\u91cf\u4e0d\u663e\u8457\uff0cOLS\u56de\u5f52\u6a21\u578b\u4e2d\u4e0d\u5b58\u5728\u5f02\u65b9\u5dee\u3002&quot;)\n\n# \u6000\u7279\u68c0\u9a8c(White Test)\n# \u5b9a\u4e49\u53d8\u91cf\ny3 = df[&quot;e^2&quot;]\ndf[&quot;Sales^2&quot;] = df[&quot;Sales&quot;]**2\nx3 = df[&quot;Sales^2&quot;]\n# \u6dfb\u52a0\u5e38\u6570\u9879\nX3 = sm.add_constant(x3)\n# \u8f85\u52a9\u56de\u5f52\nOLS_aux3 = sm.OLS(y3, X3).fit()\nprint(&quot;\u8f85\u52a9\u56de\u5f523\u7ed3\u679c\uff1a&quot;)\nprint(OLS_aux3.summary(), &quot;\\n&quot;)\ndf_test.iloc[2, 1] = OLS_aux3.fvalue.round(4)\ndf_test.iloc[2, 2] = OLS_aux3.f_pvalue.round(4)\n# F\u7edf\u8ba1\u91cf\u663e\u8457\u6027\nif OLS_aux3.f_pvalue &lt; 0.05:\n    df_test.iloc[2, 3] = &quot;\u663e\u8457&quot;\n    print(&quot;\u7ed3\u8bba\uff1a\u6000\u7279\u68c0\u9a8c(White Test)\u7684F\u7edf\u8ba1\u91cf\u663e\u8457\uff0cOLS\u56de\u5f52\u6a21\u578b\u4e2d\u5b58\u5728\u5f02\u65b9\u5dee\u3002&quot;)\nelse:\n    df_test.iloc[2, 3] = &quot;\u4e0d\u663e\u8457&quot;\n    print(&quot;\u7ed3\u8bba\uff1a\u6000\u7279\u68c0\u9a8c(White Test)\u7684F\u7edf\u8ba1\u91cf\u4e0d\u663e\u8457\uff0cOLS\u56de\u5f52\u6a21\u578b\u4e2d\u4e0d\u5b58\u5728\u5f02\u65b9\u5dee\u3002&quot;)\n\n# B-P-G\u68c0\u9a8c\uff08Breusch-Pagan-Godfrey Test\uff09\n# \u5b9a\u4e49\u53d8\u91cf\ny4 = df[&quot;e^2&quot;]\nx4 = df[&quot;Sales&quot;]\n# \u6dfb\u52a0\u5e38\u6570\u9879\nX4 = sm.add_constant(x4)\n# \u8f85\u52a9\u56de\u5f52\nOLS_aux4 = sm.OLS(y4, X4).fit()\nprint(&quot;\u8f85\u52a9\u56de\u5f524\u7ed3\u679c\uff1a&quot;)\nprint(OLS_aux4.summary(), &quot;\\n&quot;)\ndf_test.iloc[3, 1] = OLS_aux4.fvalue.round(4)\ndf_test.iloc[3, 2] = OLS_aux4.f_pvalue.round(4)\n# F\u7edf\u8ba1\u91cf\u663e\u8457\u6027\nif OLS_aux4.f_pvalue &lt; 0.05:\n    df_test.iloc[3, 3] = &quot;\u663e\u8457&quot;\n    print(&quot;\u7ed3\u8bba\uff1aB-P-G\u68c0\u9a8c\uff08Breusch-Pagan-Godfrey Test\uff09\u7684F\u7edf\u8ba1\u91cf\u663e\u8457\uff0cOLS\u56de\u5f52\u6a21\u578b\u4e2d\u5b58\u5728\u5f02\u65b9\u5dee\u3002&quot;)\nelse:\n    df_test.iloc[3, 3] = &quot;\u4e0d\u663e\u8457&quot;\n    print(&quot;\u7ed3\u8bba\uff1aB-P-G\u68c0\u9a8c\uff08Breusch-Pagan-Godfrey Test\uff09\u7684F\u7edf\u8ba1\u91cf\u4e0d\u663e\u8457\uff0cOLS\u56de\u5f52\u6a21\u578b\u4e2d\u4e0d\u5b58\u5728\u5f02\u65b9\u5dee\u3002&quot;)\nprint(&quot;\\n&quot;)\n\n# \u5f02\u65b9\u5dee\u68c0\u9a8c\u7ed3\u679c\u6c47\u603b\nprint(&quot;\u5f02\u65b9\u5dee\u68c0\u9a8c\u7ed3\u679c\u6c47\u603b\uff1a&quot;)\n# print(df_test, &quot;\\n&quot;)\nprint(df_test.to_string(index=False), &quot;\\n&quot;)\n# \u68c0\u9a8c\u7ed3\u679c\u7efc\u5408\u5224\u65ad\nif df_test[&quot;P_value&quot;].min() &lt; 0.05:\n    print(&quot;\u6700\u7ec8\u7ed3\u8bba\uff1aOLS\u6a21\u578b\u5b58\u5728\u5f02\u65b9\u5dee\u3002&quot;)\n    num = df_test[&quot;P_value&quot;].values.argmin()\n    if df_test.iloc[num, 0] == &quot;B-P-G Test&quot;:\n        print(&quot;\u5f02\u65b9\u5dee\u5f62\u5f0f\uff1a\u8bef\u5dee\u6b63\u6bd4\u4e8eX.&quot;, &quot;\\n&quot;)\n    else:\n        print(&quot;\u5f02\u65b9\u5dee\u5f62\u5f0f\uff1a\u8bef\u5dee\u6b63\u6bd4\u4e8eX\u5e73\u65b9.&quot;, &quot;\\n&quot;)\nelse:\n    print(&quot;\u6700\u7ec8\u7ed3\u8bba\uff1aOLS\u6a21\u578b\u4e0d\u5b58\u5728\u5f02\u65b9\u5dee\u3002&quot;, &quot;\\n&quot;)\n\n# \u5f02\u65b9\u5dee\u5904\u7406-WLS\u4f30\u8ba1\n# \u5f02\u65b9\u5dee\u68c0\u9a8c\uff1aB-P-G test\uff1b\u5f02\u65b9\u5dee\u5f62\u5f0f\uff1a\u8bef\u5dee\u6b63\u6bd4\u4e8ex\uff1b\u8bef\u5dee\u5904\u7406\uff1aWLS,w=sqrt(sales)\n# \u5b9a\u4e49\u53d8\u91cf\ny = df[&quot;RD&quot;]\nx = df[&quot;Sales&quot;]\n# \u5b9a\u4e49\u6743\u91cd\n# w = df[&quot;Sales&quot;]       # White test:\u8bef\u5dee\u65b9\u5dee\u6b63\u6bd4\u4e8eX\u5e73\u65b9\nw = df[&quot;Sales&quot;]**0.5    # B-P-G test:\u8bef\u5dee\u65b9\u5dee\u6b63\u6bd4\u4e8eX\n# \u6dfb\u52a0\u5e38\u6570\u9879\nX = sm.add_constant(x)\n# WLS\u56de\u5f52\nWLS_model = sm.WLS(y, X, weights=1.0\/(w**2)).fit()\nprint(&quot;WLS\u56de\u5f52\u7ed3\u679c\uff1a&quot;)\nprint(WLS_model.summary())\n\n# WLS\u4f30\u8ba1\u7ed3\u679c\u7684\u5f02\u65b9\u5dee\u68c0\u9a8c\n# \u5bf9WLS\u4f30\u8ba1\u7ed3\u679c\u7684\u5f02\u65b9\u5dee\u68c0\u9a8c\u9700\u8981\u7559\u610f\u8f85\u52a9\u56de\u5f52\u7684\u89e3\u91ca\u53d8\u91cf\u5f62\u5f0f\u3002Eviews\u8f6f\u4ef6\u4e2d\u9ed8\u8ba4\u91c7\u7528\u539f\u59cbOLS\u56de\u5f52\u7684\u89e3\u91ca\u53d8\u91cf\uff0c\u8fd9\u662f\u9519\u8bef\u7684\u3002\n# \u751f\u6210\u6b8b\u5dee\ndf[&quot;e_wls&quot;] = WLS_model.resid\n\n# B-P-G\u68c0\u9a8c\uff08Breusch-Pagan-Godfrey Test\uff09\n# \u5b9a\u4e49\u53d8\u91cf\ndf[&quot;e^2_wls&quot;] = df[&quot;e_wls&quot;]**2\ny = df[&quot;e^2_wls&quot;]\ndf[&quot;Sales_c&quot;] = df[&quot;Sales&quot;]**(-0.5)\ndf[&quot;Sales_x&quot;] = df[&quot;Sales&quot;]**0.5\nx = df[[&quot;Sales_c&quot;, &quot;Sales_x&quot;]]\n# \u6dfb\u52a0\u5e38\u6570\u9879\nX = sm.add_constant(x)\n# \u8f85\u52a9\u56de\u5f52\nOLS_aux = sm.OLS(y, X).fit()\nprint(&quot;\u8f85\u52a9\u56de\u5f52\u7ed3\u679c\uff1a&quot;)\nprint(OLS_aux.summary(), &quot;\\n&quot;)\n# F\u7edf\u8ba1\u91cf\u663e\u8457\u6027\nif OLS_aux.f_pvalue &lt; 0.05:\n    print(&quot;\u7ed3\u8bba\uff1a\u5bf9WLS\u4f30\u8ba1\u7ed3\u679c\u7684B-P-G\u68c0\u9a8c\u8868\u660e\uff0cF\u7edf\u8ba1\u91cf\u663e\u8457\uff0cWLS\u56de\u5f52\u6a21\u578b\u4e2d\u5b58\u5728\u5f02\u65b9\u5dee\u3002&quot;)\nelse:\n    print(&quot;\u7ed3\u8bba\uff1a\u5bf9WLS\u4f30\u8ba1\u7ed3\u679c\u7684B-P-G\u68c0\u9a8c\u8868\u660e\uff0cF\u7edf\u8ba1\u91cf\u4e0d\u663e\u8457\uff0cWLS\u56de\u5f52\u6a21\u578b\u4e2d\u4e0d\u5b58\u5728\u5f02\u65b9\u5dee\u3002&quot;)\nprint(&quot;\\n&quot;)\n\n# white\u68c0\u9a8c\uff08White Test\uff09\n# \u5b9a\u4e49\u53d8\u91cf\ndf[&quot;e^2_wls&quot;] = df[&quot;e_wls&quot;]**2\ny = df[&quot;e^2_wls&quot;]\ndf[&quot;Sales_c&quot;] = df[&quot;Sales&quot;]**(-1)\ndf[&quot;Sales_x&quot;] = df[&quot;Sales&quot;]\nx = df[[&quot;Sales_c&quot;, &quot;Sales_x&quot;]]\n# \u6dfb\u52a0\u5e38\u6570\u9879\nX = sm.add_constant(x)\n# \u8f85\u52a9\u56de\u5f52\nOLS_aux = sm.OLS(y, X).fit()\nprint(&quot;\u8f85\u52a9\u56de\u5f52\u7ed3\u679c\uff1a&quot;)\nprint(OLS_aux.summary(), &quot;\\n&quot;)\n# F\u7edf\u8ba1\u91cf\u663e\u8457\u6027\nif OLS_aux.f_pvalue &lt; 0.05:\n    print(&quot;\u7ed3\u8bba\uff1a\u5bf9WLS\u4f30\u8ba1\u7ed3\u679c\u7684White\u68c0\u9a8c\u8868\u660e\uff0cF\u7edf\u8ba1\u91cf\u663e\u8457\uff0cWLS\u56de\u5f52\u6a21\u578b\u4e2d\u5b58\u5728\u5f02\u65b9\u5dee\u3002&quot;)\nelse:\n    print(&quot;\u7ed3\u8bba\uff1a\u5bf9WLS\u4f30\u8ba1\u7ed3\u679c\u7684White\u68c0\u9a8c\u8868\u660e\uff0cF\u7edf\u8ba1\u91cf\u4e0d\u663e\u8457\uff0cWLS\u56de\u5f52\u6a21\u578b\u4e2d\u4e0d\u5b58\u5728\u5f02\u65b9\u5dee\u3002&quot;)\nprint(&quot;\\n&quot;)\n\n# \u5bf9\u6570\u6a21\u578b vs \u7ebf\u6027\u6a21\u578b\n# \u5b9a\u4e49\u53d8\u91cf\ndf[&quot;Log(RD)&quot;] = df[&quot;RD&quot;].apply(np.log)\ndf[&quot;Log(Sales)&quot;] = df[&quot;Sales&quot;].apply(np.log)\ny0 = df[&quot;RD&quot;]\ny1 = df[&quot;Log(RD)&quot;]\nx0 = df[&quot;Sales&quot;]\nx1 = df[&quot;Log(Sales)&quot;]\n# \u6dfb\u52a0\u5e38\u6570\u9879\nX0 = sm.add_constant(x0)\nX1 = sm.add_constant(x1)\n# OLS\u56de\u5f52\nOLS_linear = sm.OLS(y0, X0).fit()\nOLS_log = sm.OLS(y1, X1).fit()\n# \u663e\u793a\u56de\u5f52\u7ed3\u679c\nprint(&quot;\u7ebf\u6027OLS\u56de\u5f52\u7ed3\u679c\uff1a&quot;)\nprint(OLS_linear.summary(), &quot;\\n&quot;)\nprint(&quot;\u5bf9\u6570\u7ebf\u6027OLS\u56de\u5f52\u7ed3\u679c\uff1a&quot;)\nprint(OLS_log.summary(), &quot;\\n&quot;)\n\n# \u5f02\u65b9\u5dee\u68c0\u9a8c\n# \u751f\u6210\u6b8b\u5dee\ndf[&quot;e_linear&quot;] = OLS_linear.resid\ndf[&quot;e_log&quot;] = OLS_log.resid\ndf[&quot;e^2_linear&quot;] = df[&quot;e_linear&quot;]**2\ndf[&quot;e^2_log&quot;] = df[&quot;e_log&quot;]**2\n# \u5b9a\u4e49\u53d8\u91cf\ny0 = df[&quot;e^2_linear&quot;]\ny1 = df[&quot;e^2_log&quot;]\nx0 = df[&quot;Sales&quot;]\nx1 = df[&quot;Log(Sales)&quot;]\n# \u6dfb\u52a0\u5e38\u6570\u9879\nX0 = sm.add_constant(x0)\nX1 = sm.add_constant(x1)\n# OLS\u8f85\u52a9\u56de\u5f52\nOLS_linear_aux = sm.OLS(y0, X0).fit()\nOLS_log_aux = sm.OLS(y1, X1).fit()\n# \u663e\u793a\u56de\u5f52\u7ed3\u679c\nprint(&quot;\u7ebf\u6027OLS-\u8f85\u52a9\u56de\u5f52\u7ed3\u679c\uff1a&quot;)\nprint(OLS_linear_aux.summary(), &quot;\\n&quot;)\nprint(&quot;\u5bf9\u6570\u7ebf\u6027OLS-\u8f85\u52a9\u56de\u5f52\u7ed3\u679c\uff1a&quot;)\nprint(OLS_log_aux.summary(), &quot;\\n&quot;)\n# F\u7edf\u8ba1\u91cf\u663e\u8457\u6027\n\n# Linear\nif OLS_linear_aux.f_pvalue &lt; 0.05:\n    print(&quot;\u7ed3\u8bba\uff1aB-P-G\u68c0\u9a8c\u8868\u660e\uff0cF\u7edf\u8ba1\u91cf\u663e\u8457\uff0c\u7ebf\u6027OLS\u56de\u5f52\u6a21\u578b\u4e2d\u5b58\u5728\u5f02\u65b9\u5dee\u3002&quot;)\nelse:\n    print(&quot;\u7ed3\u8bba\uff1aB-P-G\u68c0\u9a8c\u8868\u660e\uff0cF\u7edf\u8ba1\u91cf\u4e0d\u663e\u8457\uff0c\u7ebf\u6027OLS\u56de\u5f52\u6a21\u578b\u4e2d\u4e0d\u5b58\u5728\u5f02\u65b9\u5dee\u3002&quot;)\nprint(&quot;\\n&quot;)\n\n# Log\nif OLS_log_aux.f_pvalue &lt; 0.05:\n    print(&quot;\u7ed3\u8bba\uff1aB-P-G\u68c0\u9a8c\u8868\u660e\uff0cF\u7edf\u8ba1\u91cf\u663e\u8457\uff0c\u5bf9\u6570OLS\u56de\u5f52\u6a21\u578b\u4e2d\u5b58\u5728\u5f02\u65b9\u5dee\u3002&quot;)\nelse:\n    print(&quot;\u7ed3\u8bba\uff1aB-P-G\u68c0\u9a8c\u8868\u660e\uff0cF\u7edf\u8ba1\u91cf\u4e0d\u663e\u8457\uff0c\u5bf9\u6570OLS\u56de\u5f52\u6a21\u578b\u4e2d\u4e0d\u5b58\u5728\u5f02\u65b9\u5dee\u3002&quot;)\nprint(&quot;\\n&quot;)\n\n# Linear vs Log\nif OLS_linear_aux.f_pvalue &lt; OLS_log_aux.f_pvalue:\n    print(&quot;\u7ed3\u8bba\uff1a\u76f8\u6bd4\u7ebf\u6027OLS\u56de\u5f52\uff0c\u5bf9\u6570OLS\u56de\u5f52\u80fd\u591f\u51cf\u7f13\u5f02\u65b9\u5dee\u7684\u5f71\u54cd\u3002&quot;)\nelse:\n    print(&quot;\u7ed3\u8bba\uff1a\u76f8\u6bd4\u7ebf\u6027OLS\u56de\u5f52\uff0c\u5bf9\u6570OLS\u56de\u5f52\u4e0d\u80fd\u51cf\u7f13\u5f02\u65b9\u5dee\u7684\u5f71\u54cd\u3002&quot;)\nprint(&quot;\\n&quot;)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4f8b\u5b50 \u4ee5\u8bfe\u672c\u886811-5\uff08\u7b2c396\u9875\uff09\u4e2d\u7684\u6570\u636e\u4e3a\u6837\u672c\uff0c\u5efa\u7acb\u5982\u4e0b\u56de\u5f52\u6a21\u578b\uff1a \u4ee3\u7801\u90e8\u5206 # lec06 # [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[29,30],"tags":[],"class_list":["post-5153","post","type-post","status-publish","format-standard","hentry","category-29","category-30"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/seit2019.xyz\/index.php?rest_route=\/wp\/v2\/posts\/5153","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/seit2019.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/seit2019.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/seit2019.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/seit2019.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5153"}],"version-history":[{"count":0,"href":"https:\/\/seit2019.xyz\/index.php?rest_route=\/wp\/v2\/posts\/5153\/revisions"}],"wp:attachment":[{"href":"https:\/\/seit2019.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/seit2019.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/seit2019.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}