{"id":4972,"date":"2022-11-14T14:44:50","date_gmt":"2022-11-14T06:44:50","guid":{"rendered":"https:\/\/seitlab.com\/?p=4972"},"modified":"2022-11-14T14:44:50","modified_gmt":"2022-11-14T06:44:50","slug":"5-3-%e8%ae%a1%e9%87%8f%e5%88%86%e6%9e%903%ef%bc%9a%e4%b8%80%e5%85%83%e7%ba%bf%e6%80%a7%e5%9b%9e%e5%bd%92%e6%a8%a1%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/www.seitlab.com\/?p=4972","title":{"rendered":"5.3 \u8ba1\u91cf\u5206\u67903\uff1a\u4e00\u5143\u7ebf\u6027\u56de\u5f52\u6a21\u578b"},"content":{"rendered":"<h3>\u4f8b\u5b502.1<\/h3>\n<p>\u4e3a\u4e86\u7814\u7a762015\u5e74\u4e2d\u56fd\u5404\u7701\u519c\u6751\u5c45\u6c11\u4eba\u5747\u6d88\u8d39\u652f\u51fa\uff08Cons\uff09\u548c\u53ef\u652f\u914d\u6536\u5165\uff08DPI\uff09\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u6211\u4eec\u4ee5\u4eba\u5747\u6d88\u8d39\u652f\u51fa\uff08Cons\uff09\u4e3a\u56e0\u53d8\u91cf\uff0c\u4ee5\u4eba\u5747\u53ef\u652f\u914d\u6536\u5165\uff08DPI\uff09\u4e3a\u81ea\u53d8\u91cf,\u8fdb\u884c\u56de\u5f52\uff0c\u4f30\u8ba1\u5982\u4e0b\u4e00\u5143\u7ebf\u6027\u56de\u5f52\u6a21\u578b\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/i0.wp.com\/seitlab.com\/wp-content\/uploads\/2020\/01\/%E4%BE%8B%E5%AD%902.1%E6%95%B0%E6%8D%AE.png?w=606&amp;ssl=1\" alt=\"\u4f8b\u5b502.1\u6570\u636e\u622a\u56fe\" \/><\/p>\n<p>\u6570\u636e\u6765\u6e90\uff1a2016\u5e74\u300a\u4e2d\u56fd\u7edf\u8ba1\u5e74\u9274\u300b \u88686-31\uff0c\u88686-29<\/p>\n<h3>\u4ee3\u7801\u90e8\u5206<\/h3>\n<pre><code># lec03\n# \u8ba1\u91cf\u5b9e\u9a8c2-\u4ee3\u7801\u90e8\u5206\n\n# \u5bfc\u5165\u5e93\u6587\u4ef6\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport statsmodels.api as sm\nfrom statsmodels.stats.diagnostic import het_white\nfrom statsmodels.stats.diagnostic import het_breuschpagan\nimport ssl\n\n# \u5bfc\u5165\u672c\u5730excel\u6570\u636e\uff08\u8def\u5f84\uff1a\u5f53\u524d\u76ee\u5f55\/data\/\uff09\n# df01 = pd.read_excel(r&quot;.\/data\/cons_rural_2020.xlsx&quot;)\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\ndf01 = pd.read_excel(r&quot;https:\/\/cdn.seitlab.com\/data\/econometrics\/cons_rural_2020.xlsx&quot;)\n\n# \u5c55\u793a\u6570\u636e\nprint(&quot;\u539f\u59cb\u6570\u636e\u5c55\u793a\u5982\u4e0b\uff1a&quot;)\nprint(df01, &quot;\\n&quot;)\n\n# \u7b80\u8981\u63cf\u8ff0\u6027\u7edf\u8ba1\u5206\u6790\nprint(&quot;\u539f\u59cb\u6570\u636e\u7684\u4e3b\u8981\u7edf\u8ba1\u6307\u6807\uff1a&quot;)\nprint(df01.describe(), &quot;\\n&quot;)\n\n# \u7ed8\u56fe\n\n# \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\n# \u7ed8\u5236\u6563\u70b9\u56fe\n\n# \u5b9a\u4e49\u53d8\u91cf\nx01 = df01[&quot;\u5730\u533a&quot;]\ny01 = df01[&quot;2020\u519c\u6751\u5c45\u6c11\u4eba\u5747\u6d88\u8d39\u652f\u51fa(\u5143)&quot;]\ny02 = df01[&quot;2020\u519c\u6751\u5c45\u6c11\u4eba\u5747\u53ef\u652f\u914d\u6536\u5165(\u5143)&quot;]\n\n# 2020\u519c\u6751\u5c45\u6c11\u4eba\u5747\u6d88\u8d39\u652f\u51fa(\u5143)\u6563\u70b9\u56fe\nplt.scatter(x01, y01, c=&quot;blue&quot;)\nplt.xlabel(&quot;\u5730\u533a&quot;)\nplt.xticks([])             # \u5173\u95edx\u8f74\u523b\u5ea6\u663e\u793a\nplt.ylabel(&quot;\u519c\u6751\u5c45\u6c11\u4eba\u5747\u6d88\u8d39\u652f\u51fa(\u5143)&quot;)\nplt.title(&quot;2020\u519c\u6751\u5c45\u6c11\u4eba\u5747\u6d88\u8d39\u652f\u51fa\u6563\u70b9\u56fe&quot;, fontsize=&quot;xx-large&quot;, fontweight=&quot;bold&quot;)\nplt.show()\n\n# 2020\u519c\u6751\u5c45\u6c11\u4eba\u5747\u53ef\u652f\u914d\u6536\u5165(\u5143)\u6563\u70b9\u56fe\nplt.scatter(x01, y02, c=&quot;green&quot;)\nplt.xlabel(&quot;\u5730\u533a&quot;)\nplt.xticks([])             # \u5173\u95edx\u8f74\u523b\u5ea6\u663e\u793a\nplt.ylabel(&quot;\u519c\u6751\u5c45\u6c11\u4eba\u5747\u53ef\u652f\u914d\u6536\u5165(\u5143)&quot;)\nplt.title(&quot;2020\u519c\u6751\u5c45\u6c11\u4eba\u5747\u53ef\u652f\u914d\u6536\u5165\u6563\u70b9\u56fe&quot;, fontsize=&quot;xx-large&quot;, fontweight=&quot;bold&quot;)\nplt.show()\n\n# \u7ed8\u5236XY\u6563\u70b9\u56fe\n\n# \u5b9a\u4e49\u53d8\u91cf\ny1 = df01[&quot;2020\u519c\u6751\u5c45\u6c11\u4eba\u5747\u6d88\u8d39\u652f\u51fa(\u5143)&quot;]\nx1 = df01[&quot;2020\u519c\u6751\u5c45\u6c11\u4eba\u5747\u53ef\u652f\u914d\u6536\u5165(\u5143)&quot;]\n\n# \u6d88\u8d39\u652f\u51fa\u4e0e\u53ef\u652f\u914d\u6536\u5165\u6563\u70b9\u56fe\nplt.scatter(x1, y1, c=&quot;black&quot;)\nplt.xlabel(&quot;\u519c\u6751\u5c45\u6c11\u4eba\u5747\u53ef\u652f\u914d\u6536\u5165(\u5143)&quot;)\nplt.ylabel(&quot;\u519c\u6751\u5c45\u6c11\u4eba\u5747\u6d88\u8d39\u652f\u51fa(\u5143)&quot;)\nplt.title(&quot;\u6d88\u8d39\u652f\u51fa\u4e0e\u53ef\u652f\u914d\u6536\u5165\u6563\u70b9\u56fe&quot;, fontsize=&quot;xx-large&quot;, fontweight=&quot;bold&quot;)\nplt.show()\n\n# \u7edf\u8ba1\u63cf\u8ff0\n\n# \u91cd\u65b0\u5b9a\u4e49\u6570\u636e:\u5254\u9664\u7b2c\u4e00\u5217,\u4fee\u6539\u5217\u540d\ndf02 = df01.iloc[:, 1:3]\ndf02 = df02.rename(columns={&quot;2020\u519c\u6751\u5c45\u6c11\u4eba\u5747\u6d88\u8d39\u652f\u51fa(\u5143)&quot;: &quot;CONS&quot;, &quot;2020\u519c\u6751\u5c45\u6c11\u4eba\u5747\u53ef\u652f\u914d\u6536\u5165(\u5143)&quot;: &quot;DPI&quot;})\nprint(&quot;\u56de\u5f52\u5206\u6790\u7684\u6570\u636e\u4e3a\uff1a&quot;)\nprint(df02, &quot;\\n&quot;)\n\n# \u5b9a\u4e49\u53d8\u91cf\ny2 = df02[&quot;CONS&quot;]\nx2 = df02[&quot;DPI&quot;]\n\n# \u5c55\u793a\u6570\u636e\u7684\u7b80\u8981\u7edf\u8ba1\u63cf\u8ff0\nprint(&quot;\u6570\u636e\u7684\u4e3b\u8981\u7edf\u8ba1\u6307\u6807\uff1a&quot;)\nprint(df02.describe(), &quot;\\n&quot;)\n\n# \u5c55\u793a\u7edf\u8ba1\u6307\u6807\n# \u5b9a\u4e49\u7edf\u8ba1\u6307\u6807\u51fd\u6570\ndef stat(x):\n    return pd.Series([x.count(), x.min(), x.quantile(.25), x.median(), x.quantile(.75), x.max(), x.mean(), x.var(), x.std(), x.skew(), x.kurt(), x.max()-x.min()], index=[&#039;\u603b\u6570&#039;, &#039;\u6700\u5c0f\u503c&#039;, &#039;\u4e0b\u56db\u5206\u4f4d\u6570&#039;, &#039;\u4e2d\u4f4d\u6570&#039;, &#039;\u4e0a\u56db\u5206\u4f4d\u6570&#039;, &#039;\u6700\u5927\u503c&#039;, &#039;\u5e73\u5747\u6570&#039;, &#039;\u65b9\u5dee&#039;, &#039;\u6807\u51c6\u5dee&#039;, &#039;\u504f\u5ea6&#039;, &#039;\u5cf0\u5ea6&#039;, &#039;\u6781\u5dee&#039;])\n\n# \u8ba1\u7b97\u7edf\u8ba1\u6307\u6807\ns1 = pd.DataFrame(stat(y2))\ns2 = pd.DataFrame(stat(x2))\n\n# \u5408\u5e76\u663e\u793a\u7edf\u8ba1\u6307\u6807\ndfs = pd.DataFrame(columns=[&#039;CONS&#039;, &#039;DPI&#039;])\ndfs[&#039;CONS&#039;] = s1\ndfs[&#039;DPI&#039;] = s2\nprint(dfs, &quot;\\n&quot;)\ndfs.to_excel(&#039;.\/data\/cons_rural_2020_stats.xlsx&#039;)\n\n# \u5c55\u793a\u6570\u636e\u7684\u534f\u65b9\u5dee\u77e9\u9635\nprint(&quot;\u6570\u636e\u7684\u534f\u65b9\u5dee\u77e9\u9635\uff1a&quot;)\nprint(df02.cov(), &quot;\\n&quot;)\n\n# \u5c55\u793a\u6570\u636e\u7684\u76f8\u5173\u7cfb\u6570\u77e9\u9635\nprint(&quot;\u6570\u636e\u7684\u76f8\u5173\u7cfb\u6570\u77e9\u9635\uff1a&quot;)\nprint(df02.corr(), &quot;\\n&quot;)\n\n# \u4e00\u5143\u7ebf\u6027\u56de\u5f52\n\n# \u6dfb\u52a0\u5e38\u6570\u9879\nX = sm.add_constant(x2)\n\n# OLS\u56de\u5f52\nOLS_model = sm.OLS(y2, X).fit()\n\n# \u663e\u793aOLS\u56de\u5f52\u7ed3\u679c\nprint(OLS_model.summary(), &quot;\\n&quot;)\ndfc = pd.DataFrame(OLS_model.params).round(2)     # \u83b7\u53d6\u56de\u5f52\u53c2\u6570,\u4fdd\u7559\u5c0f\u6570\u70b9\u540e4\u4f4d\ndfc = dfc.astype(&quot;string&quot;)                        # \u8f6c\u6362\u683c\u5f0f\u4e3a\u5b57\u7b26\u4e32\nbeta0 = dfc.iloc[0, 0]                            # \u5b9a\u4e49beta0\nbeta1 = dfc.iloc[1, 0]                            # \u5b9a\u4e49beta1\nOLS_equation = &quot;Y=&quot; + beta0 + &quot;+&quot; + beta1 + &quot;*X&quot;  # \u5b9a\u4e49\u56de\u5f52\u65b9\u7a0b\uff0c\u683c\u5f0f\uff1a\u5b57\u7b26\u4e32\nprint(&quot;\u4f30\u8ba1\u7684\u56de\u5f52\u65b9\u7a0b\u4e3a\uff1a&quot;)\nprint(OLS_equation, &quot;\\n&quot;)\n\n# \u7ed8\u5236\u8d8b\u52bf\u548c\u6563\u70b9\u56fe\ny_fitted = OLS_model.predict()\nplt.scatter(x2, y2, label=&quot;\u5b9e\u9645\u503c&quot;)\nplt.plot(x2, y_fitted, color=&quot;black&quot;, label=&quot;\u9884\u6d4b\u503c&quot;)\nplt.title(&quot;\u519c\u6751\u5c45\u6c11\u4eba\u5747\u6d88\u8d39\u652f\u51fa\u5bf9\u4eba\u5747\u53ef\u652f\u914d\u6536\u5165\u7684\u56de\u5f52&quot;, fontsize=&quot;xx-large&quot;, fontweight=&quot;bold&quot;)\nplt.xlabel(&quot;\u519c\u6751\u5c45\u6c11\u4eba\u5747\u53ef\u652f\u914d\u6536\u5165\uff08\u5143\uff09&quot;)\nplt.ylabel(&quot;\u519c\u6751\u5c45\u6c11\u4eba\u5747\u6d88\u8d39\u652f\u51fa\uff08\u5143\uff09&quot;)\nplt.text(27000, 17800, s=OLS_equation, fontsize=&quot;x-large&quot;)\nplt.legend()\nplt.show()\n\n# \u6b8b\u5dee\u68c0\u9a8c\nmodel_resid = OLS_model.resid    # \u751f\u6210\u6b8b\u5dee\n# \u56fe\u793a\u6cd5\n# \u6b8b\u5dee\u81ea\u8eab\u5e8f\u5217\nplt.scatter(x01, model_resid)\nplt.title(&quot;\u6b8b\u5dee\u6563\u70b9\u56fe&quot;, fontsize=&quot;xx-large&quot;, fontweight=&quot;bold&quot;)\nplt.xlabel(&quot;\u5730\u533a&quot;)\nplt.ylabel(&quot;\u6b8b\u5dee&quot;)\nplt.xticks([])             # \u5173\u95edx\u8f74\u523b\u5ea6\u663e\u793a\n# plt.legend()\nplt.show()\n\n# \u6b8b\u5dee\u5bf9X\u5e8f\u5217\u7684\u6563\u70b9\u56fe\nplt.scatter(x2, model_resid)\nplt.title(&quot;\u6b8b\u5dee\u5bf9X\u6563\u70b9\u56fe&quot;, fontsize=&quot;xx-large&quot;, fontweight=&quot;bold&quot;)\nplt.xlabel(&quot;\u519c\u6751\u5c45\u6c11\u4eba\u5747\u53ef\u652f\u914d\u6536\u5165\uff08\u5143\uff09&quot;)\nplt.ylabel(&quot;\u6b8b\u5dee&quot;)\n# plt.xticks([])             # \u5173\u95edx\u8f74\u523b\u5ea6\u663e\u793a\n# plt.legend()\nplt.show()\n\n# \u6b8b\u5dee\u5bf9y_fitted\u5e8f\u5217\u7684\u6563\u70b9\u56fe\nplt.scatter(y_fitted, model_resid)\nplt.title(&quot;\u6b8b\u5dee\u5bf9y_fitted\u6563\u70b9\u56fe&quot;, fontsize=&quot;xx-large&quot;, fontweight=&quot;bold&quot;)\nplt.xlabel(&quot;\u519c\u6751\u5c45\u6c11\u4eba\u5747\u6d88\u8d39\u652f\u51fa\u62df\u5408\u503c&quot;)\nplt.ylabel(&quot;\u6b8b\u5dee&quot;)\n# plt.xticks([])             # \u5173\u95edx\u8f74\u523b\u5ea6\u663e\u793a\n# plt.legend()\nplt.show()\n\n# \u5f02\u65b9\u5dee\u68c0\u9a8c\n# white test\nwhite_output = het_white(model_resid, X)\nwhite_result = pd.Series(white_output[0:4], index=[&#039;white_lm_statistic&#039;, &#039;white_lm_p_value&#039;, &#039;white_F_statistic&#039;, &#039;white_F_statistic_p_value&#039;])\nprint(&quot;White\u5f02\u65b9\u5dee\u68c0\u9a8c\u7ed3\u679c\uff1a&quot;)\nprint(white_result)\nif white_result[&#039;white_lm_p_value&#039;] &lt; 0.05:\n    print(&quot;White\u5f02\u65b9\u5dee\u68c0\u9a8c\u8868\u660e\uff1a\u5b58\u5728\u5f02\u65b9\u5dee&quot;, &quot;\\n&quot;)\nelse:\n    print(&quot;White\u5f02\u65b9\u5dee\u68c0\u9a8c\u8868\u660e\uff1a\u4e0d\u5b58\u5728\u5f02\u65b9\u5dee&quot;, &quot;\\n&quot;)\n\n# Breuschpagan test\nbp_output = het_breuschpagan(model_resid, X)\nbp_result = pd.Series(bp_output[0:4], index=[&#039;bp_lm_statistic&#039;, &#039;bp_lm_p_value&#039;, &#039;bp_F_statistic&#039;, &#039;bp_F_statistic_p_value&#039;])\nprint(&quot;BreuschPagan\u5f02\u65b9\u5dee\u68c0\u9a8c\u7ed3\u679c\uff1a&quot;)\nprint(bp_result, &quot;\\n&quot;)\nif bp_result[&#039;bp_lm_p_value&#039;] &lt; 0.05:\n    print(&quot;BreuschPagan\u5f02\u65b9\u5dee\u68c0\u9a8c\u8868\u660e\uff1a\u5b58\u5728\u5f02\u65b9\u5dee&quot;, &quot;\\n&quot;)\nelse:\n    print(&quot;BreuschPagan\u5f02\u65b9\u5dee\u68c0\u9a8c\u8868\u660e\uff1a\u4e0d\u5b58\u5728\u5f02\u65b9\u5dee&quot;, &quot;\\n&quot;)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4f8b\u5b502.1 \u4e3a\u4e86\u7814\u7a762015\u5e74\u4e2d\u56fd\u5404\u7701\u519c\u6751\u5c45\u6c11\u4eba\u5747\u6d88\u8d39\u652f\u51fa\uff08Cons\uff09\u548c\u53ef\u652f\u914d\u6536\u5165\uff08DPI\uff09\u4e4b\u95f4\u7684\u5173 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[29,30],"tags":[],"class_list":["post-4972","post","type-post","status-publish","format-standard","hentry","category-29","category-30"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.seitlab.com\/index.php?rest_route=\/wp\/v2\/posts\/4972","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.seitlab.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.seitlab.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.seitlab.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.seitlab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4972"}],"version-history":[{"count":0,"href":"https:\/\/www.seitlab.com\/index.php?rest_route=\/wp\/v2\/posts\/4972\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.seitlab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4972"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.seitlab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4972"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.seitlab.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4972"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}