<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Airater on Medium]]></title>
        <description><![CDATA[Stories by Airater on Medium]]></description>
        <link>https://medium.com/@airater?source=rss-65a785c4c90a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*ZZMHxLA_xO8QSLmh.jpg</url>
            <title>Stories by Airater on Medium</title>
            <link>https://medium.com/@airater?source=rss-65a785c4c90a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 17:01:47 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@airater/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[美國牛肉出口概況之研究]]></title>
            <link>https://medium.com/yuying%E7%9A%84%E4%BD%9C%E5%93%81%E9%9B%86/%E7%BE%8E%E5%9C%8B%E7%89%9B%E8%82%89%E5%87%BA%E5%8F%A3%E6%A6%82%E6%B3%81%E4%B9%8B%E7%A0%94%E7%A9%B6-a7d34ce132b5?source=rss-65a785c4c90a------2</link>
            <guid isPermaLink="false">https://medium.com/p/a7d34ce132b5</guid>
            <category><![CDATA[research]]></category>
            <dc:creator><![CDATA[Airater]]></dc:creator>
            <pubDate>Sat, 08 Aug 2020 11:37:28 GMT</pubDate>
            <atom:updated>2020-08-12T04:40:05.723Z</atom:updated>
            <content:encoded><![CDATA[<p><em>本研究旨在研究美國牛肉出口現狀之概況，並試圖分析其出口為何具有優勢。</em></p><blockquote><strong><em>摘要</em></strong></blockquote><p>本研究旨在研究美國牛肉出口現狀之概況，並試圖分析其出口為何具有優勢。文中蒐集美國牛肉生產模式及出口資料，運用規模經濟、H-O模型及引力模型等概念切入，分析出美國因技術、土地與養殖技術等優勢，使其擁有較低生產成本得已成為牛肉生產大國，再藉由迴歸分析得出國內生產毛額、國家間距離與美牛健康狀況確實影響美國牛肉出口量，最後探討美牛規模化生產之弊端與中國美牛走私問題。</p><blockquote><strong><em>1. 前言</em></strong></blockquote><p>美國擁有廣大的土地可用於種植牧草與玉米，而兩者皆可做為肉牛之飼料，使得美國在飼養肉牛具有成本上的極大優勢，再加上高度機械化與先進的養殖技術，不僅能大幅降低僱用員工數，同時也可享受規模經濟所帶來之利益，而美國對於瘦肉精使用的限制相較寬鬆，使美牛能較他國提早滿足市場需求。故美國藉由妥善運用資源與技術，並且搭配有效的政策，不但擁有生產成本的極大優勢，更享有良好的農產品出口發展，因此美國為當今世界的牛肉出口大國實為當之無愧。</p><p>然而美牛因工業化生產之過程使用瘦肉精與含有營養劑及雞糞的飼料導致美牛存在著食品安全上的疑慮，2003年爆發美牛狂牛症之事件，導致爾後美牛在出口上容易遭受進口國國民反對，但美牛因價格低廉及國家政策配合，美國仍為世界最大的牛肉出口國家。</p><p>本文第二節為美國牛肉之產業介紹；第三節透過規模經濟解釋美國為牛肉出口大國之原因，並以台灣與美國為例，運用H-O模型分析得出美國應專業化生產且出口使用美國相對豐富之生產要素的牛肉，再透過Pooled OLS(最小平方法)驗證引力模型在美牛產業的可行性，同時得知國內生產毛額、國家間距離與美牛健康狀況為影響美牛出口之變數；第四節為探討美牛規模化生產所產生的弊端；第五節為介紹中國因其生鮮牛肉市場規模日漸擴大，但國內生產無法滿足市場需求而產生美牛走私問題；第六節則將本文做一簡單的歸納與結論。</p><blockquote><strong><em>2. 美國牛肉之產業介紹</em></strong></blockquote><p>美國為畜牧業大國，而其中又以肉牛產業為最大的生產部分，其產值大約占了美國畜牧業的四分之一，由附表1可以看出2011–2015年，五年來美國牛肉產量佔世界產量幾乎都在20%左右的高比率，是世界牛肉產量最大的國家，美國牛肉品種高達八十多種，其中安格斯牛胴體品質好、淨肉率高、大理石花紋明顯，被認為是世界上各種專門化肉用品種中肉質最優秀的品種，因此較受市場的歡迎，也成為美國飼養最多的品種，數量佔了全部數量的60%。</p><p>肉牛養殖飼料可分為草飼以及穀飼，草飼即使用牧草作為飼料，而穀飼則使用大豆、玉米等穀物作為飼料，草飼類似素食，草飼牛的產肉量會較少；穀飼則類似葷食，穀飼牛的產肉量會較多，且油花分布均勻，賣相也較好，美國牛的養殖在成長期時大量餵以穀物，而絕大多數都是以玉米作為飼料，玉米適口性好，能量高，可大量用於牛的混合精料中，且因美國境內大多地廣人稀，有大量土地可利用於牧草種植以及穀物種植，其中可作為飼料的玉米，在美國產量最大，佔了全球的四成以上，美國種植玉米的規模龐大，美國五大湖(Great Lakes)以南地區的平原，是世界上面積最大、產量最高的玉米產區，玉米產量佔美國國內的四分之三。該地區位於溫帶，地勢平坦遼闊、土壤肥沃、水源充足、春夏氣溫高，適合玉米生長；此外地廣人稀、工業高度發達，提供實現農業生產高度機械化的穩定基礎；該地區鐵路網稠密、交通便利，有利於農產品運輸至美國各地，這樣的優勢也使美國牛肉飼料成本得以大幅降低。</p><p>美國為領先全球的已開發國家，科技應用也大幅增加肉牛養殖的效率，美國肉牛不但生產機械化程度高，大型飼草收割機械、飼料加工機械、送料機械、耕作機械也都一應俱全，並擁有許多的育肥廠以及屠宰廠，且因機械化程度高的緣故，因此所需人力相當少，中國肉類協會(2018)以赫布阿伯斯養殖場為例子，其養殖場存欄量有兩萬頭左右，該育肥場採用先進的飼養技術，借助內布拉斯加州得天獨厚的玉米飼料優勢，發展規模化養殖，極大降低了養殖成本，同時大力發展循環農業，利用先進科技節能減排，每年出欄量4.4萬頭，然而整個牛場工作人員僅為18人，這使其規模效益表現優異。</p><p>另一個例子，美國主要牛肉及糧食生產大州內布拉斯加州，是全美育肥、屠宰的中心地帶，每年肉牛的流動為400至500萬頭，該州在肉牛生產全產業鏈的各環節中（如飼料玉米的種植、存欄量、育肥量及加工量）均處於第一的地位，牛肉存欄量是其人口的四倍，主要以育肥牛為主，牛肉產量為70億磅，這些例子皆可看出美國牛肉養殖規模以及規模效益的龐大，且也證明了美國在牛肉產業擁有出口方面的優勢。</p><blockquote><strong><em>3. 理論模型</em></strong></blockquote><p>本研究試圖以規模經濟與H-O模型解釋美國為牛肉出口大國之原因，並藉由引力模型驗證與得知影響出口之變數及其效果。</p><h3>3.1 規模經濟</h3><p>大規模生產導致的經濟效益簡稱規模經濟(Economics of scale)，是指在一定的產量範圍內，隨著產量的增加，平均成本不斷下降的事實。如圖1表示，Q2為此模型的最適生產量，若超過或少於此生產量都會使得平均生產成本的上升，例如圖中Q小於Q2則他的平均成本就從C1上升至C，平均成本無法達到最低。另外此章分析會將產出設定為牛隻的出欄率。</p><p>美國牛肉的生產能達到規模化經濟經由分析後有主要三個原因。</p><p>1.美國肉牛業養殖通常採用工業化養殖，建造系統化並易於管理的牛舍，提高養殖密度外也減少許多人工管理成本，相較於中國的散戶式養殖有著更佳的生產效能。此外，美國養牛業良種化的意識高，各牧場會進行選育找出生長速度佳的牛隻做繁殖與飼養。</p><p>2.美國牛飼料以穀物為主，也就是所謂的穀飼牛，穀飼牛的特點就在它的飼養時間較短，以草飼牛來說一般飼養時間為30~36個月，而穀飼牛只需要18~24個月。此外，穀物飼料中的成分以玉米占大多數，而美國作為玉米的生產大國，在這樣的環境優勢下牧場主除了能縮短牛隻飼養時間外還壓低了許多飼料成本。</p><p>3.從表1可看出美國在歐多福斯(瘦肉精)的使用上的限制比其他國家甚至是農糧組織都來的寬鬆，將歐多福斯混進飼料中供牛隻長期食用，可以促進蛋白質的合成，增加動物的瘦肉量、少長脂肪以及減少飼料的使用量，甚至能讓肉品提早上市來降低成本。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/575/1*Kgws3bIMHTfRF3v_sds17A.png" /><figcaption>圖1 規模經濟長期平均成本</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/858/1*i1p2e-onPdiJFwADTTj4RA.png" /></figure><p>分析了上述三個原因後，我們再從數據來驗證美國牛的養殖是否達到規模經濟，從表2可以看出美國的出欄率都維持在37~38%的區間內，而中國則是從26%開始緩慢成長中，若將圖1的橫軸設定為出欄率，我們可以發現美國從2012到2016年的出欄率都維持在一個固定區間，從規模經濟的角度來看，這個出欄率就是所謂的最適規模產量，因美國養牛業知道多於或少於此出欄率就會造成成本的上升，以圖1而言即為Q2，而中國的出欄率雖低於美國但也慢慢地往最適規模產量前進，也就是圖1中的Q，而較早達到規模經濟的美國就能以較低的成本(價格)，進入中國的市場，故在2017年中國財政副部長宣佈要再度允許美牛的進口後，開始有許多開放低價美牛進口將會衝擊中國牛肉市場的報導出現。不過截至目前為止，開放美牛的進口對於中國肉品市場並沒有太大的衝擊，因為雖然中國開放了進口，但進口的限制繁多，例如進口牛肉需有履歷可追朔至出生的農場以及不可驗出歐多福斯，使得目前可進口的牛肉都以高價位的牛肉為主，美牛的出口優勢自然就減弱許多。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/844/1*niMuSmfeL1YQ685mSbL9dQ.png" /></figure><h3>3.2 H-O模型－以台美為例</h3><p>H-O模型是1919年由ELI.Heckscher和Bertil Ohlin所提出的理論模型，此模型認為每個國家所擁有的生產要素豐富程度並不相同，不同的商品生產時所需要的生產要素也不相同，在此情形之下，一國應專業化生產，並出口使用本國相對豐富之生產要素的商品，進口的則是使用本國相對稀少之生產要素的商品。</p><p>美國牛的養殖屬於「大規模工廠化畜牧業」，美國土地資源豐富、資金技術實力雄厚，但勞動資源缺乏，因此則實行以規模化、機械化、設備化為主要特徵的養殖方式，所以美國牛的養殖屬於資本密集型產業。</p><p>假設世界上只有台灣跟美國兩國，其生產技術皆相同，而生產要素則只有勞力及資本兩種，由表3可得出美國的K/L=393.43，而台灣的K/L=240.75，美國相較於台灣屬於資本相對豐富國家，而牛肉的生產屬於資本密集型產業，依據HO模型，美國應出口牛肉，而台灣則應進口牛肉。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/889/1*XW5U3FWJ4oHz05PIMH1VlA.png" /></figure><h3>3.3 引力模型</h3><p>引力模型(gravity model)在國際貿易之分析上，經常被用來探討兩國間貿易量，此模型指出兩國之間貿易量會受到國內生產毛額(gross domestic price ,GDP)與兩國間的距離影響，基本的引力模型以下式表示 :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/880/1*lDHvNWkitqhSpwi6emT7Fw.png" /></figure><p>式中各符號之意義:𝑇𝑖𝑗為i國與j國的貿易量、𝐴為常數,𝑌𝑖為i國的國內生產毛額、𝑌𝑗為j國的國內生產毛額與𝐷𝑖𝑗為i國與j國兩國間的距離｡</p><p>將引力模型應用於實證迴歸分析，我們把式(1)各變數取對數後得出式(2)，示之如下。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/833/1*OI5u7FpLvy-R8GfElgPS2A.png" /></figure><p>本研究欲探討的美國牛肉主要出口國為台灣、日本、南韓、中國、加拿大與墨西哥，加入各國的虛擬變數於式(2)，分別定義為Taiwan、Japan、South Korea、China、Canada與Mexico。Damodar N. Gujaratic &amp; Dawn C. Porter (2009)若直接將此六個虛擬變數引入迴歸式，將會陷入虛擬變數陷阱(dummy variable trap)，導致完全共線性(perfect multicollinearity)的問題產生，故本文視台灣為基準類(base)，刪除Taiwan變數，針對式(2)改寫為式(3)，示之如下。</p><pre>Damodar N. Gujaratic &amp; Dawn C. Porter (2009)的原文敘述</pre><pre>1. If a qualitative variable has mcategories, introduce only (m-1) dummy variables. If you do not follow this rule, you will fall into what is called the dummy variable trap, that is, the situation of perfect collinearity or perfect multicollinearity.</pre><pre>2. The category for which no dummy variable is assigned is known as the base, benchmark, control, comparison, reference, or omitted category.</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/880/1*6WR7ZKp9XPO0gpFxnw7wvw.png" /></figure><p>由於2003年12月25日爆發美牛狂牛病之事件，導致美國牛肉的主要出口國於次年紛紛停止或減少進口量，由附表2可知在2004年台灣、2004–2005年日本與中國明顯“大幅度＂減少對美牛的進口量，林麗芳(2011)表示南韓於2003年12月27日起禁止美國牛肉進口，於2006年9月8日重新開放美國牛肉進口，故2004年台灣EX為1，2004–2005年日本與中國的EX為1，2004–2006年南韓的EX為1，其餘期間EX值皆為0。由於本研究中的log(𝑌𝑖)對於各個美牛出口國為固定值，故刪除Log(𝑌𝑖)並同時新增EX變數於式(3)得出式(4)，示之如下。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/864/1*7MKDA4O3v5Pjvh60Kk9qWg.png" /></figure><p>式(4)變數定義與資料來源如表4所示，將式(4)使用OLS(最小平方法)的Pooling模型進行迴歸分析，其結果如表5呈現。</p><p>根據表5，可得知Log𝑌𝑗為正向顯著，GDP越大的國家，其經濟規模越大，國內總需求越大，在其他條件不變之下，對美牛的進口量就越大；Log𝐷𝑖𝑗為負向顯著，兩國間的距離越大，其交易成本(如運輸成本)越高則不利於貿易進行，對美牛的進口量因而減少；EX為負向顯著，代表美牛狂牛症爆發確實使其出口量減少。解釋力(Adjusted R-squared)達到0.734，代表此模型具有一定的解釋力，也意味著引力模型在實證上是可被驗證的，其結果完全符合經濟直覺。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/887/1*9Zky9j6MF1EFjAWJKV2DIg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/893/1*9__S-i-uStn-M5HdMMK04w.png" /></figure><blockquote><strong><em>4. 規模化生產之弊端</em></strong></blockquote><p>3.1節得出美牛擁有成本上的優勢成為全球牛肉出口大國之結論，但事事凡有利與弊，以下開始探討美牛生產規模化下的各種弊端。</p><p>首先是美國採取工業化養殖提升養殖密度後卻容易造成牛隻間若有疫情發生，再度交叉傳染的可能性也大大地增加，因此養殖業者必須對牛隻們在施打抗生素，且因牛隻的抗藥性越來越強，施打的抗生素的種類也會不斷地變換，故在美牛飼育期短短14個月內可能就被施打了不只10種的抗生素，使得美國牛成為名符其實的藥罐子。</p><p>再來就是穀飼牛的問題，牛隻天生的消化系統是用來消化草料而非穀物及高蛋白，使用穀物養殖已違反牛隻的天性，不僅殘害消化系統，也使其抵抗力下降，故穀物飼養也是造成美牛造成藥罐子的成因之一。</p><p>此外，穀物飼料的成分中除了玉米外還有營養劑、雞糞等令人聞之色變的添加物，這些添加物也是造成狂牛病的原因之一。</p><p>最後就是討論度最高的瘦肉精問題，雖然截至目前為止沒有因為吃了含有瘦肉精的美國牛肉而中毒的例子，但仍有許多報告指出瘦肉精有其風險性，況且亞洲人的飲食習慣中對於內臟的食用較普遍，而這種毒物殘留最嚴重的部位就是在內臟裡，故對亞洲人來說美牛對健康的威脅更是需要考量的問題，不過相較之下瘦肉精問題雖呼聲高，但嚴重度反而是較低的，也因為這些問題的存在，使得美國雖然是牛肉出口大國，但其出口歷程卻充斥著許多爭議及風波。</p><p>這時我們開始思考若禁止美牛的自由貿易是否就能夠解決上述的紛爭，減少國際市場的需求那麼美牛的養殖可能就會更人道化，也不會因為要增肉而使用瘦肉精，在直覺上似乎是正確的作法，但這時可能就犯下所謂的次佳理論，要真正解決問題不應該是停止自由貿易，而是應該要求美國養牛業者改良其養殖模式，而美國的檢驗及防疫部門該要做更嚴謹的要求，各進口國的檢驗及防疫部門也要做到更嚴格的把關，才是真正讓風波不斷的美牛問題解決的重點。</p><blockquote><strong><em>5. 中國牛肉市場走私問題</em></strong></blockquote><p>根據圖2，目前中國生鮮牛肉市場規模日漸擴大，但中國在牛肉這塊的生產並還無法達到自身的需求，須依靠進口彌補其差額，而進口又分成正規途徑以及走私，若正規途徑的進口也無法滿足缺額，亦或是走私牛肉能以更低的成本進入市場時，黑市交易的問題就會因而產生。</p><p>根據中國國家統計局的數據，2017年5月，中國的平均牛肉價格為每斤34元人民幣，另外新浪財金(2017)報導指出走私牛肉平均價格每斤約23元人民幣，這中間的價差使得走私商人有了進場的空間，這些低價的走私牛肉進入市場後雖然讓消費者可以享有較便宜的肉品但也陷入食品安全的危機。</p><p>除此之外政府也必須動用大量資源來做查緝，使得國家社會成本的上升，在2017年美國農業部預測美國當地牛肉均價為每英擔約116至126美元，換算下來每斤約10元人民幣，故若開放更多的美牛進口量則能將走私牛肉擠出市場，使得中國消費者不但能享受更便宜的牛肉，更不需煩惱牛肉的來源不明，故從美牛的案例中我們認為自由貿易的開放是能夠有效降低走私問題，讓消費者能買到便宜並具有保障的商品。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/773/1*Q3oqXwPRS7c9GSPkwEmNXg.png" /><figcaption>圖2 2009–2017年中國生鮮牛肉市場規模走勢圖</figcaption></figure><blockquote><strong><em>6. 結論</em></strong></blockquote><p>牛肉作為美國飲食文化最為廣泛使用的食材之一，其龐大的市場需求量促使美國在畜牧業的集約化及工業化的生產較他國提早發展，因此美國在生產技術上遙遙領先許多國家，而美國在玉米、大豆等穀物的生產能力也是這世界上佼佼者，綜合以上兩大原因，美國成為牛肉生產大國，在近年來隨著許多國家對於牛肉的需求也日夜俱增的情況下，美國也更躍升成為了牛肉出口大國。然而美國為了維持其牛肉出口的優勢，更加精進了養殖方面的技術，政府也訂下了比多數國家寬鬆的瘦肉精殘留量，使得養殖業者能夠擁有更好的生產優勢以利出口。</p><p>我們從引力模型分析可瞭解美牛的出口狀況，其主要出口國家以有地緣關係的墨西哥以及加拿大外，還有亞洲較高GDP的國家如日本、南韓以及中國，可見美牛的貿易狀況非常符合引力模型的假設，此外我們可從附表1中發現2003年美牛因狂牛病疫情導致出口量大幅下降，美牛的食品安全爭議也從那時開始受到國際間的關注。</p><p>美國牛肉達成了規模化經濟的最適產量，能夠以其優越的成本優勢進入其他國家的市場，但其規模化的過程違反牛的天性的養殖方法，故當狂牛病問題爆發後，許多美牛進口國家也立即對美牛下禁止令，雖然近年美國政府加強對於疫情的把關，以往的進口國也陸續開放對美國牛肉的進口，但在這些國家的人民心中，美國牛肉的形象已一去不復返，故要如何在降低成本以及食品安全中抓好平衡，美國政府必須要找到解決方法並對養殖業者們進行輔導，才可能重拾國外消費者們的信心。</p><p>另外我們也發現中國牛肉市場若開放美國牛肉的進口，對於走私市場的衝擊大，故打擊走私問題也成了中國願意開方美國牛肉進口的誘因之一。</p><h3>附錄</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/701/1*nqbldJkQ4WavWspiBGmGDQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/701/1*ccgSH-rzUjrGVwoXAYcwQQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/702/1*vUiEfx-hujgGLEaZrn8IFw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/709/1*xYSEO_5rfG6nSP8B2gAvhw.png" /></figure><h3>參考文獻</h3><p>中國肉類協會(2018),「美國牛肉產業考察報告」, <a href="http://www.chinameat.org/index.php?a=detail&amp;id=1337">http://www.chinameat.org/index.php?a=detail&amp;id=1337</a></p><p>林麗芳(2011),「美韓牛肉爭端：問題與現況」,《農政與農情》,229</p><p>新浪財金(2017),「美國牛肉14年後再輸華：中國不吃虧能打擊走私牛肉」, <a href="http://finance.sina.com.cn/china/gncj/2017-06-22/doc-ifyhmtcf2737828.shtml">http://finance.sina.com.cn/china/gncj/2017-06-22/doc-ifyhmtcf2737828.shtml</a></p><p>Damodar N. Gujaratic &amp; Dawn C. Porter (2009), “Basic Econometrics,”McGraw-Hill Education,2 Penn Plaza, New York</p><p>Flying distances between 325major airports in the World, <a href="http://www.etn.nl/distanc4.htm">http://www.etn.nl/distanc4.htm</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a7d34ce132b5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/yuying%E7%9A%84%E4%BD%9C%E5%93%81%E9%9B%86/%E7%BE%8E%E5%9C%8B%E7%89%9B%E8%82%89%E5%87%BA%E5%8F%A3%E6%A6%82%E6%B3%81%E4%B9%8B%E7%A0%94%E7%A9%B6-a7d34ce132b5">美國牛肉出口概況之研究</a> was originally published in <a href="https://medium.com/yuying%E7%9A%84%E4%BD%9C%E5%93%81%E9%9B%86">Works</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[2001年到2017年間影響台灣央行干預行為之因素研究]]></title>
            <link>https://medium.com/yuying%E7%9A%84%E4%BD%9C%E5%93%81%E9%9B%86/2001%E5%B9%B4%E5%88%B02017%E5%B9%B4%E9%96%93%E5%BD%B1%E9%9F%BF%E5%8F%B0%E7%81%A3%E5%A4%AE%E8%A1%8C%E5%B9%B2%E9%A0%90%E8%A1%8C%E7%82%BA%E4%B9%8B%E5%9B%A0%E7%B4%A0%E7%A0%94%E7%A9%B6-fdff9f02ab7?source=rss-65a785c4c90a------2</link>
            <guid isPermaLink="false">https://medium.com/p/fdff9f02ab7</guid>
            <category><![CDATA[research]]></category>
            <dc:creator><![CDATA[Airater]]></dc:creator>
            <pubDate>Sat, 08 Aug 2020 10:56:43 GMT</pubDate>
            <atom:updated>2020-08-08T10:56:43.531Z</atom:updated>
            <content:encoded><![CDATA[<p>基於楊雅惠與許嘉棟所著《新台幣匯率與央行干預行為》一文，做更進一步地探討。</p><blockquote><em>1. 研究動機</em></blockquote><p>根據楊雅惠,許嘉棟(2005)《新台幣匯率與央行干預行為》指出，央行之所以會對外匯進行干預是為了穩定本國匯率、促進經濟成長與穩定物價，並因此取用了可以代表這三者的變數來放入迴歸式中代替干預項。</p><p>由此，我們感到好奇，除了楊雅惠,許嘉棟(2005)所用到的資料外，是否還有其他變數可以加入用來解釋干預，使干預的估計更為準確。因此，我們決定建構一個外匯干預決定式，來驗證是否有更佳的變數可以用來估計干預。</p><p>除此之外，由於楊雅惠,許嘉棟(2005)所引用的資料年代（1987–2002）與本文的資料年代（2001–2017）幾乎沒有重疊，因此，我們亦想探討是否隨著年代的改變，變數對干預的影響會有所不同。</p><blockquote><em>2. 相關文獻回顧</em></blockquote><p>楊雅惠,許嘉棟(2005)《新台幣匯率與央行干預行為》主要研究外匯干預是否在新臺幣匯率的決定上有足夠的影響力，而根據實證過後，所得出的結論是外匯干預對新台幣匯率決定有相當的影響力，因此在考量新臺幣匯率決定式時，應放入外匯干預項。</p><p>由於我國央行實際外匯干預數值不公開，因此，楊雅惠、許嘉棟(2005)利用央行進行外匯干預的三個原因（促進經濟成長、穩定物價、穩定匯率）來代表外匯干預。</p><p>在建立模型時，楊雅惠、許嘉棟(2005)分別建立有無外匯干預的匯率決定式，並在有外匯干預的決定式時以景氣領先綜合指標（LEAD）來代表景氣、在物價與匯率方面則採用美國、日本、韓國。在實證分析時，為了簡化分析則僅保留美臺相對物價，而省略與日本、韓國的。</p><p>而後根據迴歸式進行OLS分析的結果，促進經濟成長、穩定物價、穩定前期新台幣匯率三項變數都對匯率有顯著的影響，且考量此三者（外匯干預）的迴歸式亦較不考量對新臺幣匯率時有較高的解釋能力，故證實匯率受干預影響。</p><p>本文基於楊雅惠、許嘉棟(2005)對外匯干預的代替項上，結合李秀雲(2018)的資料來建構外匯干預決定式，並探討除了楊雅惠、許嘉棟(2005)對於外匯干預代替項所引用的變數之外，是否有其他變數也可以用來當作干預式的影響項。</p><blockquote><em>3. 研究手法</em></blockquote><h3>3.1 央行干預三大政策目標</h3><p>根據中央銀行年報(2010)，央行採取管理式浮動匯率制，亦即央行會適時地干預外匯市場，買進外匯將會使新台幣貶值，賣出外匯則使新台幣升值。楊雅惠,許嘉棟(2005)指出央行干預的三項政策目標變數－促進經濟成長、穩定物價與穩定匯率，其實證研究指出經濟不景氣時央行會讓新台幣貶值、國內物價膨脹時央行會讓新台幣升值與央行藉由買賣外匯使匯率波動幅度減緩，也就是說，物價變動、前期新台幣匯率變動、景氣波動皆會讓央行進場干預。</p><p><strong>(1)穩定物價</strong></p><p>國內物價通膨時，本國國民多消費進口品，央行為了穩定物價，將進場賣出外匯，干預金額為負，故國內物價與干預金額成負向關係，在此使用消費者物價上漲率(GCPI)表示國內物價。</p><p><strong>(2)穩定前期新台幣匯率</strong></p><p>楊雅惠,許嘉棟(2005)指出央行藉由部份調整模式緩和匯率波動，因此央行會參照前期新台幣匯率進行干預，當前期匯率升時，當期匯率也會上升，但由於央行會同向部分調整匯率波動，不會讓匯率上升幅度太大，亦即使本期匯率稍微下降(升值)，而央行透過賣出外匯達成此目的，此時干預金額為負，故前期新台幣匯率與干預金額為負向關係。</p><p><strong>(3)刺激出口以刺激經濟成長</strong></p><p>楊雅惠,許嘉棟(2005)指出當期景氣表現較差時，央行會透過讓新台幣貶值的方式刺激出口，此時干預金額為正，故景氣與干預金額呈負向關係，由於楊楊雅惠,許嘉棟(2005)的論文研究區間為西元1987年至2002年，其使用的景氣領先指標綜合指數(LEAD)源自於經建會，但因經建會已於2014年改制為國發會，而國發會公布的景氣領先指標綜合指數不只一種，所以我們選擇了以成長循環為概念，去除長期趨勢的領先指標不含趨勢指數作為LEAD，其預測“未來景氣＂之變動。</p><h3>3.2 央行干預迴歸式</h3><p>綜合上述的央行干預三大政策目標，即可得出式(1)央行干預迴歸式。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/894/1*QYtwWxupwZlsDAgg_vrfOg.png" /></figure><p>式(1)變數定義與預期影響如表1所示。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/902/1*McyB5lJnZJ6IQhiQ1LMg5A.png" /></figure><p>將實證數據帶入式(1)，得到其模型解釋力(AdjustedR2)，為了提高此模型解釋力，本文試圖納入幾個變數於式(1)進行探討。</p><h3>3.3 加入其他影響干預行為的變數</h3><p><strong>(1)換匯點</strong></p><p>將換匯點(遠期匯率與即期匯率的差額)視為變數納入央行最適干預迴歸式得出式(5)。若遠期外匯(f)大於即期匯率(E)表示美元溢價，亦即民眾預期新台幣將會貶值；若遠期外匯(f)小於即期匯率(E)表示美元折價，亦即民眾預期新台幣將會升值。柯百鈴,劉維琪(1998)《我國中央銀行外匯干預之訊息散佈效果》中指出人民預期匯率將會影響即期匯率，而央行干預的目標是穩定匯率，因此當遠期匯率大於(小於)即期匯率時，央行會賣出(買進)外匯使外匯市場形成超額供給(需求)，迫使新台幣升值(貶值)，干預金額為負(正)，故遠期匯率與即期匯率的差額與干預金額呈現負向關係。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/893/1*4bXMp1Anc-kXbJeetA5CFA.png" /></figure><p>式(2)變數定義與預期影響如表2所示。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/921/1*ZF8_OHKtRkbw10LfI9cgAg.png" /></figure><p><strong>(2)工業生產指數</strong></p><p>因為領先指標不含趨勢指數具領先景氣變動之性質，故可預測“未來景氣＂之變動，而工業生產指數成長率代表當時的景氣狀況，可衡量當前景氣，工業生產指數成長率上升對“當期景氣＂有正面影響，此時干預金額為負，故工業生產指數成長率與干預金額呈現負向關係，加入式(1)得出式(3)。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/886/1*9lDH6IB1soiu5BxocDnwew.png" /></figure><p>式(3)變數定義與預期影響如表3所示。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/912/1*nSOw-UrmCM1paN2y_Ap09A.png" /></figure><p><strong>(3)出口成長率</strong></p><p>出口成長率上升對當期景氣有正面影響，此時干預金額為負，加入央行最適干預迴歸式得出式(4)。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/861/1*X7Q29VfWbP6mNj4HW3VnGQ.png" /></figure><p>式(4)變數定義與預期影響如表4所示。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/882/1*jResOIU5Hq3RaooCTMyEBg.png" /></figure><blockquote><strong><em>4.實證資料</em></strong></blockquote><p>資料區間為2001.01至2017.12，皆使用月資料。</p><h3>4.1 被解釋變數</h3><p>(1)央行干預金額（INTV）</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/878/1*rYmaIuC_LWSL3E6KpEN_ww.png" /><figcaption>圖一、干預金額(百萬美元)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/786/1*PvTo9PEe2gLlwmOg6z-k2g.png" /></figure><h3><strong>4.2 解釋變數</strong></h3><p>(2)前一期新台幣匯率（𝐒−𝟏）</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/883/1*vygR3l_PtSzqTNPSD5unXw.png" /><figcaption>圖二、前一期新台幣匯率</figcaption></figure><p><strong>(3)消費者物價上漲率（GCPI）</strong></p><p>其數值定義為</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/863/1*NoaEzO5u4TOAcI_muYJn8g.png" /></figure><p>CPI為消費者物價指數(基期:民國105 年=100)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/879/1*HcGRootGav3bPFQbjUgG0g.png" /><figcaption>圖三、消費者物價指數上漲率(%)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/782/1*Bv0ocR5NGsOqwFh-bhXi2A.png" /></figure><p><strong>(4)領先指標不含趨勢指數（LEAD）</strong></p><p>其數值定義為</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/886/1*IkgVmjb1MaDjoIzgp4KZRQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/883/1*bDwXfHLTj3dys2LComxcmQ.png" /><figcaption>圖四、領先指標不含趨勢指數</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/777/1*HXbayrYBiijscnXrcd2OIA.png" /></figure><p><strong>(5)美元兌新台幣遠期匯率(30天)(賣出匯率)與新台幣匯率的差額（fE）</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/880/1*QfmUvzHcylIWDFHsXfswBQ.png" /><figcaption>圖五、美元兌新台幣遠期匯率-新台幣匯率</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/884/1*U2BZqL_gHFx5CYpo5xzKZg.png" /></figure><p><strong>(6)工業生產指數成長率（GIP）</strong></p><p>其數值定義為</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/901/1*zqODrbhAWFArnH3bY_hjbQ.png" /></figure><p>IP為工業生產指數(連鎖指數:民國105 年= 100)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/879/1*NIZ5fazc7MBIacpdagBNXQ.png" /><figcaption>圖六、工業生產指數成長率(%)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/874/1*c5y4S4lbQICAy7NtZ_NJuA.png" /></figure><p><strong>(7)出口成長率(GEXR)</strong></p><p>其數值定義為</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/830/1*yurXgaqynDCEaiwjnsIDCw.png" /></figure><p>EX表出口總值，為出口＋復出口（復出口係指外貨進口經簡易加工、轉售或因故退回之再出口），單位為百萬美元。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/882/1*gz6cBq1TrPirLYAm5M_Dhw.png" /><figcaption>圖七、出口成長率(%)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/849/1*fJI_XedFRcU3YagHJrhcaA.png" /></figure><blockquote><strong><em>5.實證模型</em></strong></blockquote><p>由於本研究使用時間序列資料，因此我們先檢定所有變數是否符合定態數列，結果如表12所示。用ADF單根檢定資料是否有單根，虛無假設為非定態，若P值小於0.1則拒絕虛無假設，視序列為定態。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/888/1*9hUYp_kXr-um8rCgCnERdQ.png" /></figure><p>由表一可得知，前一期新台幣匯率(S−1)為非定態時間序列，因此我們此此變數做一階差分（定義為dS−1），試圖使資料呈現定態，結果如表二所示。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/902/1*pTc1KyVu3nM4C8Xu8fkzZw.png" /></figure><p>結果顯示資料經一階差分後，其P值小於0.1，拒絕虛無假設(非定態)，為定態數列，因此迴歸分析時前一期新台幣匯率本文將採用取對數一階差分後的資料。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/878/1*Jqt9_7g-ev4FEaJ8jf7Muw.png" /></figure><p>將式(1)用OLS做迴歸後，結果如表14呈現。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/874/1*Q4zxoC6ikvxEhcZsIbB-0g.png" /></figure><p>前面的研究手法段落，我們預期消費者物價上漲率、領先指標不含趨勢指數與各國前一期匯率對央行干預金額有負向影響。而迴歸結果與我們推估的相同。</p><p>接下來，我們納入換匯點(fE)於式(1)得出式(2)，試圖使模型的解釋力提高。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/890/1*R4T60v4YnYSnf__82aLSOw.png" /></figure><p>將式(2)用OLS做迴歸後，結果如表15呈現。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/897/1*76Qw4sityNGF6WqBsXCmfg.png" /></figure><p>加入換匯點(fE)後，模型的Adjusted R-squared從0.1086提高至0.1794，因此得證換匯點(fE)是影響干預的變數，但迴歸結果顯示換匯點(fE)對干預的影響卻是正向的，這跟我們的推測不符合，箇中原因我們不太清楚，但可能是預估時作出了錯誤的假設、推論，或者是資料的選取不是最佳的。</p><p>納入GIP於式(1)得到式(3)，試圖使模型的解釋力提高。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/853/1*XycaCUbDPzrgKWhMKFVdag.png" /></figure><p>將式(3)用OLS做迴歸後，結果如表16呈現。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/855/1*EQXKnZZ4TWf9m64NqGI8AQ.png" /></figure><p>我們發現，加入GIP後模型的Adjusted R-squared從0.1086提高至0.1137，其解釋力的變化不大，且GIP的迴歸係數對比其他變數對干預的影響也小上不少，故我們推測或許GIP對干預的確如我們預期般有影響，但因影響不如我們預期的大，再加上GIP跟LEAD兩者雖然一個是未來景氣，一個是當期景氣，但可能會交互作用而產生影響，所以導致迴歸結果是正向影響。</p><p>納入GEXR於式(1)得到式(4)，試圖使模型的解釋力提高。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/867/1*VkDrhsFuUXj38lrDhBElAA.png" /></figure><p>將式(4)用OLS做迴歸後，結果如表17呈現。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/856/1*yRAH8wcdd69xVADYalGNNA.png" /></figure><p>將GEXR加入模型反而使Adjusted R-squared從0.1086下降至0.106，這代表GEXR對干預其實是沒有什麼影響的，甚至幾乎沒有關係，才會導致解釋力下降。因此，雖然我們推測GEXR會影響干預，但實際上卻不會，故在此就不贅述為何GEXR的係數與我們所推估的不同，因為沒有統計上的意義。</p><p>再將換匯點(fE)與GIP都加入式(1)，看能否使模型的解釋力達到更高。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/841/1*4EhaAT8CZXY9JyPNCtrHVA.png" /></figure><p>將式(5)用OLS做迴歸後，結果如表18呈現。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/864/1*lESlsgw-9RsR1pv8WhzCPg.png" /></figure><p>由上面迴歸的結果可以看到，當我們將fE與GEXR都加入模型時，Adjusted R-squared由0.1086上升到0.1798，雖然是所有式子中最高的，但相較只加入換匯點的式(2)來說，只比它高了0.004，其增加幅度可以忽略不記。我們認為這表示現在景氣對干預的影響不大，可以不納入干預決定式中，因此，最佳干預決定式為式(2)。</p><blockquote><strong><em>6. 結論</em></strong></blockquote><p>雖然本研究的實證資料使用區間皆為2001年至2017年的月資料，與楊雅惠、許嘉棟(2005)的實證資料區間(1987年至2002年)幾乎不重疊，但本研究發現消費者物價上漲率、前期新台幣匯率與景氣領先指標綜合指數(LEAD)皆對央行干預產生負向影響，此結果不僅符合我們的預期，同時也符合楊雅惠、許嘉棟(2005)的實證結果，亦即此三者變數不會因時期不同而讓央行的干預行為產生改變。</p><p>換匯點(fE)不符合我們原先預期結果，其對央行干預產生正向的影響，其中可能原因為本研究選用的資料並非為最佳或者是我們的預期影響的推論有誤，若將美元兌新台幣遠期匯率(30天)(賣出匯率)更改成無本金遠期外匯交易(NDF)所用的NDF匯率，或許能解決此問題。</p><p>迴歸式(1)加入工業生產指數成長率後，得到的式(3)Adjusted R-squared僅上升0.0051，亦即工業生產指數成長率對央行干預的影響並沒有我們先前預期的大，且其影響為正向的，與我們的預估不符，可能是因為代表未來景氣的領先指標不含趨勢指數(LEAD)與代表當期景氣的工業生產指數成長率彼此交互影響，導致實證結果與預期不符。</p><p>出口成長率(GEXR)不符合我們原先預期結果，其對央行干預產生正向的影響，迴歸式(1)加入出口成長率後，得到的式(4)Adjusted R-squared下降0.0026，代表出口成長率對央行干預並非如我們認為的會造成影響，是沒有什麼影響力的。</p><p>綜上所述，我們可以得出央行干預會受到消費者物價上漲率、前期新台幣匯率與景氣領先指標綜合指數(LEAD)、換匯點影響的結論。此結果的前三者符合央行干預的三大政策目標－穩定物價、緩和匯率波動、刺激景氣，至於換匯點可能因為資料選取並非最適，所以與我們的預期產生了偏差，但仍可見其對干預的影響力。而其他變數則可能因變數間的交互影響、影響力量不如預期等原因，不能完全解釋央行干預行為。</p><h3><strong>參考文獻</strong></h3><p>李秀雲(2018),《建構台灣的外匯市場干預資料》, 中正大學經濟學系草稿。</p><p>中央銀行年報(2010), 中央銀行。</p><p>賴景昌(2007), 《國際金融理論基礎篇》, 華泰文化事業股份有限公司，第二章第八節, 頁43–48、第四章第三節, 頁93–98。</p><p>楊雅惠、許嘉棟(2005),《新台幣匯率與央行干預行為》, 台灣經濟預測與政策, 中央研究院經濟研究所編輯, 第35 卷第2 期, 頁23–41。</p><p>柯百鈴,劉維琪(1998), 《我國中央銀行外匯干預之訊息散佈效果》, 亞太管理評論, 第3卷第2期, 頁197–208。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fdff9f02ab7" width="1" height="1" alt=""><hr><p><a href="https://medium.com/yuying%E7%9A%84%E4%BD%9C%E5%93%81%E9%9B%86/2001%E5%B9%B4%E5%88%B02017%E5%B9%B4%E9%96%93%E5%BD%B1%E9%9F%BF%E5%8F%B0%E7%81%A3%E5%A4%AE%E8%A1%8C%E5%B9%B2%E9%A0%90%E8%A1%8C%E7%82%BA%E4%B9%8B%E5%9B%A0%E7%B4%A0%E7%A0%94%E7%A9%B6-fdff9f02ab7">2001年到2017年間影響台灣央行干預行為之因素研究</a> was originally published in <a href="https://medium.com/yuying%E7%9A%84%E4%BD%9C%E5%93%81%E9%9B%86">Works</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Study: Who Is Slipping Through the Unemployment Cracks?]]></title>
            <link>https://medium.com/yuying%E7%9A%84%E4%BD%9C%E5%93%81%E9%9B%86/study-who-is-slipping-through-the-unemployment-cracks-5e95513db9d0?source=rss-65a785c4c90a------2</link>
            <guid isPermaLink="false">https://medium.com/p/5e95513db9d0</guid>
            <category><![CDATA[analysis]]></category>
            <dc:creator><![CDATA[Airater]]></dc:creator>
            <pubDate>Mon, 27 Jul 2020 06:48:15 GMT</pubDate>
            <atom:updated>2020-07-27T06:48:15.335Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/970/1*YcpSILYVScBsUa6Z4iQ3oQ.jpeg" /></figure><blockquote><strong><em>1. Link for the original article</em></strong></blockquote><p><a href="https://www.usnews.com/news/economy/articles/2017-08-18/study-who-is-slipping-through-the-unemployment-cracks">Study: Who Is Slipping Through the Unemployment Cracks?</a></p><blockquote><strong><em>2. Summary</em></strong></blockquote><p>Trump alleged America’s unemployment rate didn’t tell the whole story. Although the government counts anyone without a job who has actively looked for work in the past four weeks as unemployed, it also misses a key segment of the U.S. population that just isn’t looking for a job anymore. Furthermore, the gender ratios among nonparticipants become more imbalanced as education increases and labor force participation is the key channel through which Americans contribute to and benefit from their economy, making it vital that we understand who is left out of the labor force.</p><blockquote><strong><em>3. Comments</em></strong></blockquote><p>Here we want to discuss whether the America’s unemployment rate shows the complete story of labor market and point out wrong which appears in this article.</p><blockquote>1. Is the America’s unemployment rate telling the whole story?</blockquote><p>“Don’t believe these phony numbers,” Trump told supporters early last year. “The number is probably 28, 29, as high as 35 percent. In fact, I even heard recently 42 percent.” That is what Trump said during his election campaign. But how the number 42% came out.</p><p>If you counted everyone who’s not in the labor force as “unemployed,” you would get a higher jobless rate. But that’s obviously inflated, as it includes retirees, college students and voluntary stay-at-home parents.</p><p>For example, the U.S. has following data on September last year. We denote <em>Ut</em> as Trump’s version of unemployment rate.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/1*MWjVopq1Qblosb9-wPIkNg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/677/1*J15GCZnmjG_BDd45ApZxjg.png" /></figure><p>If we calculate<em> U3</em> and <em>Ut</em> directly in the aspect of their definitions as showed below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/503/1*bQCaQ1bnCNhTg10rPUvuIg.png" /></figure><p>From the data of <em>U3</em> and <em>Ut</em> from 2007 to 2016 (Figure 1), we can see <em>Ut </em>is always higher than <em>U3 </em>and <em>Ut-U3 </em>is increasing after financial crises in 2008. (Figure 2) It indicates △X1&gt;△X2. The increase of labor force is larger than the increase of not in labor forces in each year. This is what Trump tells us and the outcome seems to be useless to explain the whole story of the America’s unemployment rate. Trump himself didn’t tell the entire story of the national unemployment rate.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/675/1*9eoqF5S0vnQykRspIeqBoA.png" /><figcaption>Figure 1</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/678/1*9CyE0jVXS7Q_Bob7aEmx-A.png" /><figcaption>Figure 2</figcaption></figure><p>However, we all know The Bureau of Labor Statistics issues six unemployment rate indicators, namely U1, U2, U3 … U6. And U3 is the government announced the official unemployment rate.</p><p>▸U1 : Percentage of labor force unemployed 15 weeks or longer.</p><p>▸U2 : Percentage of labor force who lost jobs or completed temporary work.</p><p>▸U3 : Official unemployment rate per the International Labour Organization definition occurs when people are without jobs and they have actively looked for work within the past four weeks.</p><p>▸U4 : U3 + “discouraged workers”, or those who have stopped looking for work because current economic conditions make them believe that no work is available for them.</p><p>▸ U5 : U4 + other “marginally attached workers”, or “loosely attached workers”, or those who “would like” and are able to work, but have not looked for work recently.</p><p>▸U6 : U5 + part-time workers who want to work full-time, but cannot due to economic reasons.</p><p>If we put the cart before the horse, having 6 types of unemployment rate obviously tells us the national unemployment rate (<em>U3</em>) doesn’t show the complete information for sure.</p><p>Take financial crises happened in 2008 for instance. Financial crises make people unemployed and shift to work part-time. From Figure 4, we can see part-time employees are clearly increasing after 2008. If we just simply use <em>U3 </em>to explain the labor market, it can’t show us the change of people working part-time. That is also the reasons why we add <em>U6</em> to express the phenomenon.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/672/1*o1mDo9SCCz9tYfu-Gs_OLw.png" /><figcaption>Figure 3</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/1*lt5OTwgFvJdmU_ikEbhdTw.png" /><figcaption>Figure 4</figcaption></figure><blockquote>2. In this article, it says the gender ratios among nonparticipants become more imbalanced as education increases. However, it’s not true.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/679/1*4pNu82PodndHTZzlhCdJDw.png" /><figcaption>Figure 5</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/675/1*tO3LfoXtzY_FJF2oCQLC5Q.png" /><figcaption>Figure 6</figcaption></figure><p>From the data we get from Bureau of Labor Statistics in the U.S. (Figure 5 and 6) If we take average numbers of Less than a high school diploma(x1), High school graduates, no college (x2), Some college or associate degree (x3) and Bachelor’s degree and higher(x4) from 2007 to 2016. We can have the outcome showed below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/772/1*dyFUyLOCZJpHHpJ_deLOIA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/675/1*h11chn_KTVLKv0mkIra0Tw.png" /><figcaption>Figure 7</figcaption></figure><p>The gender ratio among nonparticipants is not always increasing when the education level is higher. (Figure 7) It indicates the relation between sex and education level isn’t highly correlative all the time.</p><blockquote><strong><em>4. Data sources</em></strong></blockquote><p>Data are obtained from Bureau of Labor Statistics (United States Department of Labor).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5e95513db9d0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/yuying%E7%9A%84%E4%BD%9C%E5%93%81%E9%9B%86/study-who-is-slipping-through-the-unemployment-cracks-5e95513db9d0">Study: Who Is Slipping Through the Unemployment Cracks?</a> was originally published in <a href="https://medium.com/yuying%E7%9A%84%E4%BD%9C%E5%93%81%E9%9B%86">Works</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Debt-to-GDP Ratio: How to Calculate and Use It]]></title>
            <link>https://medium.com/yuying%E7%9A%84%E4%BD%9C%E5%93%81%E9%9B%86/debt-to-gdp-ratio-how-to-calculate-and-use-it-67cde7afe2bc?source=rss-65a785c4c90a------2</link>
            <guid isPermaLink="false">https://medium.com/p/67cde7afe2bc</guid>
            <category><![CDATA[analysis]]></category>
            <dc:creator><![CDATA[Airater]]></dc:creator>
            <pubDate>Sun, 26 Jul 2020 13:07:26 GMT</pubDate>
            <atom:updated>2020-07-26T13:07:26.859Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/375/1*JRyGi8VNo0lNOnV7xnnW_w.png" /></figure><blockquote><strong><em>1. Link for the original article</em></strong></blockquote><p><a href="https://www.thebalance.com/debt-to-gdp-ratio-how-to-calculate-and-use-it-3305832">How to Tell When a Country Has Too Much Debt</a></p><blockquote><strong><em>2. Summary</em></strong></blockquote><p>Wanting to know a country’s economy, we always check its GDP at first. However, the debt-to-GDP ratio is also critical for quickly understanding how strong a country’s economy is. It allows people to judge a country’s ability to pay off its debt and compare debt levels between countries. “The debt-to-GDP ratio is the ratio between a country’s government debt (a cumulative amount) and its GDP (measured in years)”. (Wiki) Nevertheless, the ratio is not always a good predictor of which country will default. The situations are unlike whether a country’s debts owned by foreign investors, governments and banks or not. Generally speaking, the ratio is a good rule of thumb that points out how likely a country is to use good faith to pay off its debt.</p><blockquote><strong><em>3. Comments</em></strong></blockquote><p>According to a study by the World Bank, if the debt-to-GDP ratio exceeds 77 percent for an extended period of time, it slows economic growth. Every percentage point of debt above this level costs the country 1.7 percent in economic growth. Based on the study, it seems that a country having a high debtto-GDP ratio for a long time must definitely have a recession. However, it’s not exactly correct.</p><p>Here we have two countries both having high debt-to-GDP ratio but the situations are totally different between these two countries.</p><p>1.The first one is Greece. As the demand of Greek bonds goes down, it leads to the price decrease. It means that the debt-to-GDP rate would go up due to the market interest rate. Once the debt-to-GDP rate is higher, the cost of financing activities for Greece will become larger. Then, the financial crisis comes out. This would make the government carry out some severe policies for fiscal austerity. It might result in the panic from citizens and the economic collapse. In the end, it may cause the investors distrust to the ability of repaying debt of Greece.</p><p>2.The second one is Japan. According to the data, the debt-to-GDP ratio in Japan is 228%. However, the government of Japan doesn’t need to worry about the debt. That’s because most of the government’s debt held by its citizens, not foreigners. Compared with Greece, Greek government’s debt are mostly held by foreign governments and banks. As Greece’s bank notes became due, its debts were downgraded by ratings agencies.</p><p>There are also countries having relatively high debt-to-GDP rates but investors still invest their money in them. Take the U.S. for instance.</p><p>The U.S. debt-to-GDP ratio is 104.8% which is higher than China, Germany and the United Kingdom. However, the most of investors believe the risk of default is very low. The reasons are the U.S. can issue debt in its own currency and yield on the U.S.’s debt is always low.</p><p>1.The U.S. can issue debt in its own currency and it can simply “print” more dollars to pay off the debt. The word “print” mentioned here is not literal meaning. It means that Fed can carry out quantitative easing to increase money supply when the economy is in a recession. QE enables the central bank to pour money to the banking system to maintain interest rates at very low standards. When the Fed adds credit, it gives the banks more than they need in reserves. Banks then seek to make a profit by lending the excess to other banks. The Fed also lowered the interest rate banks charge and this is known as the fed funds rate.</p><p>2.Yield on the U.S.’s debt is always low which indicates the demand of the U.S.’s debt is usually high. The phenomenon is also identical to the theoretical literature on global imbalances that emphasizes the role of U.S. financial markets Although the exact mechanism varies across models, one key theme in recent research is that lower levels of financial market development in other countries will continue to support capital flows into the United States, thereby supporting the U.S. current account deficit and large global imbalances without major changes in asset prices.</p><p>In conclusion, with a wisely use and fully understanding of the debt-to-GDP, the ratio is still a great tool to know a country’s ability to pay off its debts and its current economy.</p><blockquote><strong><em>4. Reference</em></strong></blockquote><p>Wiki debt-to-GDP ratio</p><p><a href="https://en.wikipedia.org/wiki/Debt-to-GDP_ratio">Debt-to-GDP ratio</a></p><p>商周 別被債務占GDP比騙了</p><p><a href="http://www.businessweekly.com.tw/article.aspx?id=737&amp;type=Blog&amp;p=2">別被債務占GDP比騙了－大師開講｜商周</a></p><p>天下雜誌 投資界不敗的美國隊長是？</p><p><a href="http://www.cw.com.tw/article/article.action?id=5076014">財經台／投資界不敗的美國隊長是？｜天下雜誌</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=67cde7afe2bc" width="1" height="1" alt=""><hr><p><a href="https://medium.com/yuying%E7%9A%84%E4%BD%9C%E5%93%81%E9%9B%86/debt-to-gdp-ratio-how-to-calculate-and-use-it-67cde7afe2bc">Debt-to-GDP Ratio: How to Calculate and Use It</a> was originally published in <a href="https://medium.com/yuying%E7%9A%84%E4%BD%9C%E5%93%81%E9%9B%86">Works</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Japan’s yen rises after N Korea warning]]></title>
            <link>https://medium.com/yuying%E7%9A%84%E4%BD%9C%E5%93%81%E9%9B%86/japans-yen-rises-after-n-korea-warning-f9234cac863f?source=rss-65a785c4c90a------2</link>
            <guid isPermaLink="false">https://medium.com/p/f9234cac863f</guid>
            <category><![CDATA[analysis]]></category>
            <dc:creator><![CDATA[Airater]]></dc:creator>
            <pubDate>Sun, 26 Jul 2020 12:49:36 GMT</pubDate>
            <atom:updated>2020-07-26T12:49:36.277Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*rIeR3CKUefre_Al9APpNgQ.jpeg" /></figure><blockquote><strong><em>1. Link for the original article</em></strong></blockquote><p><a href="http://www.bbc.com/news/business-41356777">Japan&#39;s yen rises after N Korea warning</a></p><blockquote><strong><em>2. Abstract</em></strong></blockquote><p>Japanese Yen rises after North Korea tested a bomb. In the article, they are trying to figure out some reasons to explain the phenomenon. They also take some examples to compare with the risk of bomb testing and analysis why investors are still willing to invest on Yen instead of other currency, such as the U.S. dollars.</p><blockquote><strong><em>3. Comments</em></strong></blockquote><p>It’s not unusual to see Japan’s currency climbing during a crisis. There are three main reasons to explain the phenomenon.</p><p>1. Investors think that the armed conflict is barely possible. What investors really care about is that the possibility of the trade blockade or the reduction in the sale of real estate or cars.</p><p>2. People always want to increase their cash reserve when confronting the financial and economic risk. However, people nowadays have less good options to keep their money somewhere safe. The Eurozone is in a mess now. Switzerland and Singapore are not big enough to keep a great deal of money. Moreover, due to the change of the president and the possibility of reelection of the chair of the Fed, the U.S. dollars have been no longer attractive and safe for the investors to keep and flee the currency. Yet, there are some other methods to keep people’s wealth safe such as purchasing gold. If some of inventors buy gold instead of fleeing Japan’s yen, perhaps the yen won’t climb so obviously.</p><p>3. The interest rate of yen has been very low for a long time. Simply to say, Yen is not sensitive to the change of the economic environment. Hence, investors have a high tendency to use the yen to invest the high-risk assets. However, when facing the economic or financial risk, investors will change their money into Japan’s yen which lead to the increase of the interest rate.</p><p>With these three reasons, Yen can rise after North Korea suggested it might test a hydrogen bomb over the Pacific Ocean. However, we all know that the business cycles are caused by random productivity shocks. Thus, if there are any wars or armed conflicts, they will lead to negative TFP (Total Factor Productivity) shocks. The equilibrium real output, real wage and employment will decrease consequently. Simultaneously, the real interest rate and price level will increase. (Figure 1.2.3) As a result, wars and armed conflicts are not good for the economic environment no matter how much investors gain from them.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/767/1*2v4Dw9SetGtjeLGTpx2bEw.png" /><figcaption>Figure 1. labor market and output market</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/445/1*no-n9Y-lh346h2W0V18AoA.png" /><figcaption>Figure 2. monetary market</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*MmlRsA9g25LDKASHB1Sg1g.png" /><figcaption>Figure 3. Analyze</figcaption></figure><p>In conclusion, it may sound a little bit illogical at first when hearing that a currency can rise owing to the danger. Nevertheless, after taking a close look at the reasons, it’s no wonder that investors are willing to keep their money in Japan.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f9234cac863f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/yuying%E7%9A%84%E4%BD%9C%E5%93%81%E9%9B%86/japans-yen-rises-after-n-korea-warning-f9234cac863f">Japan’s yen rises after N Korea warning</a> was originally published in <a href="https://medium.com/yuying%E7%9A%84%E4%BD%9C%E5%93%81%E9%9B%86">Works</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[Python] 台灣兒童死亡數之線性模型預測 with code]]></title>
            <link>https://medium.com/yuying%E7%9A%84%E4%BD%9C%E5%93%81%E9%9B%86/python-%E5%8F%B0%E7%81%A3%E5%85%92%E7%AB%A5%E6%AD%BB%E4%BA%A1%E6%95%B8%E4%B9%8B%E7%B7%9A%E6%80%A7%E6%A8%A1%E5%9E%8B%E9%A0%90%E6%B8%AC-with-code-fe0dc1ee2dff?source=rss-65a785c4c90a------2</link>
            <guid isPermaLink="false">https://medium.com/p/fe0dc1ee2dff</guid>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Airater]]></dc:creator>
            <pubDate>Sun, 26 Jul 2020 02:54:02 GMT</pubDate>
            <atom:updated>2020-07-26T02:54:02.935Z</atom:updated>
            <content:encoded><![CDATA[<p><em>本文使用Python以OLS進行分析。</em></p><blockquote><strong><em>1. 研究動機</em></strong></blockquote><p>鑒於台灣長期的低生育率無法藉由各項福利政策的提供達到顯著提升，在生育率低迷的情況下，我國兒童死亡數又比部份先進國家高，因此兒童死亡數是比生育率更需被關注的議題。</p><blockquote><strong><em>2. 研究目標</em></strong></blockquote><p>探討經濟情況與女性識字率是否跟兒童死亡數具有密切關係。</p><blockquote><strong><em>3. 研究方法</em></strong></blockquote><p>以 Damodar N. Gujaratic &amp; Dawn C. Porter (2009)當作實證研究之基礎，利用其中人均 GNP 與女性識字率對兒童死亡數關係之模型。</p><p>兒童死亡數模型建立在以下預測式：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/363/1*HAVWYWOvsTGuBIlSzu7POA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/551/1*oMCjq0J8oOdMWyQSIGgiWg.png" /></figure><p><strong>在實證過程中以 A 代替</strong>𝑪𝑴𝒊<strong>、B 代替</strong>𝑷𝑮𝑵𝑷𝒊<strong>、C 代替</strong>𝑭𝑳𝑹𝒊<strong>。</strong></p><blockquote><strong><em>4. 資料來源</em></strong></blockquote><p>本文以民國 86 年至 107 年之台灣資料作為實證資料，其中台灣每一千個零到四歲的兒童內之死亡人數取自於內政部統計處，台灣人均 GNP(名目值，美元)與台灣女性識字率皆取自於行政院主計總處。</p><blockquote><strong><em>5. 實作</em></strong></blockquote><h3>Step 1 : 匯入資料</h3><pre>import pandas as pd<br>deathdf=pd.read_excel(&quot;final.xlsx&quot;)<br>X=deathdf[[&quot;B&quot;,&quot;C&quot;]]<br>y=deathdf[&quot;A&quot;]</pre><pre># 檢視前5筆 features X<br>X.head() #2個特徵(解釋變數)<br># 檢視前5筆 target y<br>y.head()</pre><pre>#print(X.shape) #2維 (22,2)<br>#print(y.shape) #1維 (22,)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/227/1*aufEDRRftoAH25JxEKOrgA.png" /><figcaption>X</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/309/1*l94Y6zx81HMT6gqUewuZNA.png" /><figcaption>y</figcaption></figure><h3>Step 2 : 資料探勘(EDA)</h3><pre>import matplotlib.pyplot as plt<br>%matplotlib inline</pre><pre># 以 pd.plotting.scatter_matrix() 繪製散佈圖矩陣<br>EDA1=[&quot;A&quot;,&quot;B&quot;,&quot;C&quot;]<br>pd.plotting.scatter_matrix(deathdf[EDA1],figsize(15,15),marker=&#39;o&#39;,s=30,alpha=0.8)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/646/1*7vsMCPCO4D7XFPvBfgij6Q.png" /></figure><p>由散佈圖可看出 A 與 B 、 A 與 C 呈負相關；B 與 C 呈正相關。</p><pre># 使用Shapiro-Wilk test檢定是否為常態<br>from scipy import stats<br>a=stats.shapiro(deathdf[&quot;A&quot;])<br>print(a)<br>b=stats.shapiro(deathdf[&quot;B&quot;])<br>print(b)<br>c=stats.shapiro(deathdf[&quot;C&quot;])<br>print(c)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/439/1*teyjZ5y8snl3ocOBApvEig.png" /></figure><p>在顯著水準0.05之下，A的p值為0.10028890520334244、B的p值為0.09918190538883209與C的p值為0.1737908124923706，皆大於0.05，無法拒絕虛無假設為常態，故三者皆為常態分配。</p><pre># 相關係數<br>deathdf.corr()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/314/1*h3zxDukkE4ySWCvst8R_Hg.png" /></figure><p>A、B與C三者皆呈高度線性相關，除了B與C成正相關外，其餘皆為負相關。</p><h3>Step 3 : 建立模型</h3><pre>import statsmodels.formula.api as smf</pre><pre>result=smf.ols(formula=&quot;A ~ B + C&quot;,data=deathdf).fit()<br>print(result.summary())<br>#print(result.params) #只取係數</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/832/1*DlPjmC1YgOBAGqPVSyqaHQ.png" /></figure><p>由上面的結果可看出C達到統計上顯著，且根據係數數值，得知C與A呈現高度相關，故繪製兩者間的散佈圖。</p><pre>plt.scatter(deathdf[&quot;C&quot;],deathdf[&quot;A&quot;])<br>plt.xlabel(&quot;C&quot;)<br>plt.ylabel(&quot;A&quot;)<br>plt.title(&quot;Relationship between C and A&quot;)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/550/1*LWe0JPDsoPQHFTrKQRIe8w.png" /></figure><h3>Step 4 : 預測</h3><pre># 由於statsmodels沒有predict函數，故使用sklearn<br>from sklearn.linear_model import LinearRegression<br>X=deathdf[[&quot;B&quot;,&quot;C&quot;]]<br>y=deathdf[&quot;A&quot;]<br>lmodel=LinearRegression().fit(X,y)<br>predicted_y=lmodel.predict(X)<br>predicted_y[:5]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/567/1*Wmp3ccfPJ7f2ByQysbBC6w.png" /></figure><pre># 繪製真實值與預測值的散佈圖<br>plt.scatter(y,predicted_y)<br>plt.xlabel(&quot;y&quot;)<br>plt.ylabel(&quot;predicted_y&quot;)<br>plt.title(&quot;y vs Predicted y&quot;)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/389/1*c0OG8OzS3BodpyzeXbmelA.png" /></figure><h3>Step 5 : 評估</h3><pre># 產生訓練與測試資料集<br>from sklearn.model_selection import train_test_split<br>Xtrain,Xtest,ytrain,ytest=train_test_split(X,y,random_state=0)</pre><pre># 由於statsmodels沒有score函數，故使用sklearn<br>from sklearn.linear_model import LinearRegression<br>X=deathdf[[&quot;B&quot;,&quot;C&quot;]]<br>y=deathdf[&quot;A&quot;]<br>lmodel=LinearRegression().fit(Xtrain,ytrain)</pre><pre># 評估模型績效 (測試資料集)<br>lmodel.score(Xtest,ytest)<br># 評估模型績效 (訓練資料集)<br>lmodel.score(Xtrain,ytrain)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/176/1*YSl65HLDJTfn6A5u-xYJSg.png" /><figcaption>模型績效 (測試資料集)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/178/1*YQfDJTDKgFBro_Sc4yMDpQ.png" /><figcaption>模型績效 (訓練資料集)</figcaption></figure><blockquote><strong><em>6. 結論</em></strong></blockquote><p>迴歸結果的 Adjusted R-squared 為 0.943，代表預測式解釋力強；從散佈圖與相關 係數可得知「台灣人均 GNP(名目值，美元)」與「台灣女性識字率」越高，「台 灣每一千個零到四歲的兒童之死亡人數」則越低；使用.params 得到「台灣女性 識字率」是影響「台灣每一千個零到四歲的兒童之死亡人數」最重要的變數。 最後以 LinearRegression 訓練模型，此模型績效高(0.9633888090131341)。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fe0dc1ee2dff" width="1" height="1" alt=""><hr><p><a href="https://medium.com/yuying%E7%9A%84%E4%BD%9C%E5%93%81%E9%9B%86/python-%E5%8F%B0%E7%81%A3%E5%85%92%E7%AB%A5%E6%AD%BB%E4%BA%A1%E6%95%B8%E4%B9%8B%E7%B7%9A%E6%80%A7%E6%A8%A1%E5%9E%8B%E9%A0%90%E6%B8%AC-with-code-fe0dc1ee2dff">[Python] 台灣兒童死亡數之線性模型預測 with code</a> was originally published in <a href="https://medium.com/yuying%E7%9A%84%E4%BD%9C%E5%93%81%E9%9B%86">Works</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[Python] 產品銷售量預測 with code (Kaggle Top 6%)]]></title>
            <link>https://medium.com/yuying%E7%9A%84%E4%BD%9C%E5%93%81%E9%9B%86/python-%E7%94%A2%E5%93%81%E9%8A%B7%E5%94%AE%E9%87%8F%E9%A0%90%E6%B8%AC-with-code-kaggle-top-6-8a15f852e874?source=rss-65a785c4c90a------2</link>
            <guid isPermaLink="false">https://medium.com/p/8a15f852e874</guid>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Airater]]></dc:creator>
            <pubDate>Wed, 22 Jul 2020 15:45:57 GMT</pubDate>
            <atom:updated>2020-07-26T07:17:14.189Z</atom:updated>
            <content:encoded><![CDATA[<p><em>本文使用Python以XGBoost進行產品銷售量預測。</em></p><blockquote><strong><em>1.本文流程</em></strong></blockquote><pre><strong>Step 0 import</strong></pre><pre><strong>Step 1 匯入資料</strong></pre><pre><strong>Step 2 資料前處理</strong></pre><pre>Step 2.1 刪除離群值<strong><br></strong>⋆「train」資料集中的「item_cnt_day」欄位<br>⋆「train」資料集中的「item_price」欄位</pre><pre>Step 2.2 處理不合理資料<strong><br></strong>⋆「train」資料集中的「item_cnt_day」有小於1<br>⋆「train」資料集中的「item_price」有負值</pre><pre>Step 2.3 資料簡化與填補<br>➤「shops」資料集，商店店名簡化。<br>1.「shop_id」=0 與「shop_id」=57<br>2.「shop_id」=1 與「shop_id」=58<br>3.「shop_id」=39 與「shop_id」=40<br>➤「shop」資料集，商店店名填補<br>1.「shop_id」=10 與「shop_id」=11</pre><pre>Step 2.4 資料拆解<br>➤「shops」資料集拆解成「city」與「category」<br>1.資料預處理<br>2.將「shops」資料集拆解成「city」與「category」<br>3.shops」資料集中「city」欄位，將資料 &quot; !Якутск &quot; 改為 &quot; Якутск &quot;<br>4.「shops」資料集中「category」欄位，出現頻率較低者(設為小於4)放在 &quot; etc &quot;<br>➤「cats」資料集「item_category_name」欄位拆解成「type_code」，以便後續編碼<br>1.將「cats」資料集「item_category_name」欄位拆解成「type_code」<br>2.「cats」資料集中「type_code」欄位，出現頻率較低者(設為小於4)放在 &quot; etc &quot;<br>➤「items」資料集「item_name」欄位有 &quot; ! &quot;、&quot; *** &quot;、&#39;&#39; ( ) &#39;&#39;、&quot; [ ] &quot;等符號</pre><pre>Step 2.5 資料編碼<br>➤「shops」資料集<br>➤「cats」資料集<br>1.「cats」資料集「type_code」欄位編碼<br>2.「cats」資料集「item_category_name」欄位切割<br>3.「cats」資料集「subtype」欄位編碼<br>➤「items」資料集<br>1.新增「type」欄位，將常出現的品項列入其中<br>2.將Dataframe「group_sum」出現頻率較低(設為小於40)者列為 &quot;etc&quot;，並套用到「items」資料集「name2」欄位<br>3.「items」資料集「name2」欄位與「name3」欄位編碼</pre><pre><strong>Step 3 產生資料集</strong></pre><pre>Step 3.1 從「train」資料集製作 Dataframe「matrix」<br>Step 3.2 Dataframe「matrix」新增欄位－「item_cnt_month」<br>Step 3.3 將「test」資料集合併至 Dataframe「matrix」，並填上0<br>Step 3.4 將「shops」、「items」與「cats」資料集合併至 Dataframe「matrix」<br>Step 3.5 Dataframe「matrix」新增欄位－「item_cnt_month」落後3期<br>Step 3.6 Dataframe「matrix」新增欄位－「date_avg_item_cnt」落後1期<br>Step 3.7 Dataframe「matrix」新增欄位－「date_item_avg_item_cnt」落後3期<br>Step 3.8 Dataframe「matrix」新增欄位－「date_shop_avg_item_cnt」落後3期<br>Step 3.9 Dataframe「matrix」新增欄位－「date_shop_item_avg_item_cnt」落後3期<br>Step 3.10 Dataframe「matrix」新增欄位－「date_shop_subtype_avg_item_cnt」落後1期<br>Step 3.11 Dataframe「matrix」新增欄位－「date_city_avg_item_cnt」落後1期<br>Step 3.12 Dataframe「matrix」新增欄位－「date_item_city_avg_item_cnt」落後1期<br>Step 3.13 Dataframe「matrix」新增欄位－「delta_price_lag」<br>Step 3.14 Dataframe「matrix」新增欄位－「delta_revenue」落後1期<br>Step 3.15 Dataframe「matrix」新增欄位－「days」、「item_shop_first_sale」與「item_first_sale」</pre><pre><strong>Step 4 產生訓練與測試資料集</strong></pre><pre><strong>Step 5 建立模型與預測</strong></pre><pre><strong>附圖 : 在Kaggle上的成績</strong></pre><blockquote><strong><em>2. 資料來源</em></strong></blockquote><p>本文使用 Kaggle 提供之數據，給定俄羅斯最大的軟體公司之一 1C Company 的2013 年 1 月至 2015 年 10 月之商店 ID、物品 ID、物品種類 ID、單店單物物品銷售量，欲預測次月公司產品總銷售量。</p><p>詳細資料說明請點選<a href="https://www.kaggle.com/c/competitive-data-science-predict-future-sales/data">官方連結</a>，內有詳細說明，在此不再贅述。</p><blockquote><strong><em>3. 實作</em></strong></blockquote><h3>Step 0 import</h3><pre>import pandas as pd<br>import datetime<br>import seaborn as sns<br>import matplotlib.pyplot as plt<br>import numpy as np</pre><h3>Step 1 匯入資料</h3><pre>items = pd.read_csv(&#39;C:/Users/b1234/Downloads/items.csv&#39;)<br>train = pd.read_csv(&#39;C:/Users/b1234/Downloads/sales_train.csv&#39;)<br>test = pd.read_csv(&#39;C:/Users/b1234/Downloads/test.csv&#39;)<br>cats = pd.read_csv(&#39;C:/Users/b1234/Downloads/item_categories.csv&#39;)<br>shops = pd.read_csv(&#39;C:/Users/b1234/Downloads/shops.csv&#39;)</pre><h3>Step 2 資料前處理</h3><pre># 將「train」資料集的日期格式從「日－月－年」改成「年－月－日」<br>train[&#39;date&#39;] = pd.to_datetime(train.date,format=&quot;%d.%m.%Y&quot;)</pre><blockquote><strong>Step 2.1 刪除離群值</strong></blockquote><p>⋆「train」資料集中的「item_cnt_day」欄位</p><pre># 畫箱型圖判斷離群值<br>sns.set_style(&quot;whitegrid&quot;)<br>sns.boxplot(x=train.item_cnt_day)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/475/1*plW0-aoeaucHz6v3R2Y9WQ.png" /></figure><pre># 「train」中的「item_cnt_day」欄位僅保留小於1000的資料<br>train = train[train.item_cnt_day&lt;1000]</pre><p>⋆ 「train」資料集中的「item_price」欄位</p><pre># 畫箱型圖判斷離群值<br>sns.boxplot(x=train.item_price)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/472/1*E62YCV3Nok7q7IKFonr0PQ.png" /></figure><pre># 「train」中的「item_price」欄位僅保留小於100,000的資料<br>train = train[train.item_price&lt;100000]</pre><blockquote>Step <strong>2.2 處理不合理資料</strong></blockquote><p>⋆ 「train」資料集中的「item_cnt_day」有小於1</p><pre># 列出「train」資料集中的「item_cnt_day」有小於1者<br>print(train[train.item_cnt_day &lt; 1])</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/707/1*cKrYL2Moy01_cX4FYoEHkA.png" /></figure><pre># 由於「item_cnt_day」為月產品銷售量，單品月銷售量小於1不合常理，故將小於1的資料全設為0。<br>train.loc[train.item_cnt_day &lt; 1, &quot;item_cnt_day&quot;] = 0</pre><p>⋆ 「train」資料集中的「item_price」有負值</p><pre># 列出「train」資料集中的「item_price」有負值者<br>print(train[train.item_price&lt;0])</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/701/1*PY2CgKCNYLVMYJEFICthHw.png" /></figure><pre># 由於「item_price」為商品價格，商品價格為負不合常理，故使資料僅保留商品價格為正值者，並重設index。<br>train = train[train.item_price &gt; 0].reset_index(drop = True)</pre><blockquote>Step<strong> 2.3 資料簡化與填補</strong></blockquote><p>⋆ 「shops」資料集，商店店名簡化。</p><p>1. 「shop_id」=0 與「shop_id」=57</p><pre>shops[shops.shop_id == 0]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/377/1*AsmxJzT_WNpPCHjTRLY6ug.png" /></figure><pre>shops[shops.shop_id == 57]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/340/1*q4OXeNYnqZaPoNexphPUdA.png" /></figure><pre># 將「train」與「test」資料集中「shop_id」為0者全改為名稱較短的「shop_id」為57者。<br>train.loc[train.shop_id == 0, &quot;shop_id&quot;] = 57<br>test.loc[test.shop_id == 0 , &quot;shop_id&quot;] = 57</pre><p>2. 「shop_id」=1 與「shop_id」=58</p><pre>shops[shops.shop_id == 1]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/383/1*PtrfeKMH5DTXjnCD9oQFiA.png" /></figure><pre>shops[shops.shop_id == 58]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/335/1*9FzLhB4ilVId1mzPuwrKig.png" /></figure><pre># 將「train」與「test」資料集中「shop_id」為1者全改為名稱較短的「shop_id」為58者。<br>train.loc[train.shop_id == 1, &quot;shop_id&quot;] = 58<br>test.loc[test.shop_id == 1 , &quot;shop_id&quot;] = 58</pre><p>3. 「shop_id」=39 與「shop_id」=40</p><pre>shops[shops.shop_id == 39]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/457/1*9wTFcI7LRvOKkZl9PIoAuQ.png" /></figure><pre>shops[shops.shop_id == 40]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/546/1*dv1LLovEFNvQelmf0V_oIw.png" /></figure><pre># 將「train」與「test」資料集中「shop_id」為40者全改為名稱較短的「shop_id」為39者。<br>train.loc[train.shop_id == 40, &quot;shop_id&quot;] = 39<br>test.loc[test.shop_id == 40, &quot;shop_id&quot;] = 39</pre><p>⋆ 「shop」資料集，商店店名填補</p><p>1. 「shop_id」=10 與「shop_id」=11</p><pre>shops[shops.shop_id == 10]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/363/1*H1XbuXQ8wNHpyXbBNeebYQ.png" /></figure><pre>shops[shops.shop_id == 11]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/350/1*Su60xq1Ooadk5a05Qsvgdw.png" /></figure><pre># 將「train」與「test」資料集中「shop_id」為10者全改為有完整名稱的「shop_id」為10者。<br>train.loc[train.shop_id == 11, &quot;shop_id&quot;] = 10<br>test.loc[test.shop_id == 11, &quot;shop_id&quot;] = 10</pre><blockquote>Step<strong> 2.4 資料拆解</strong></blockquote><p>⋆ 「shops」資料集拆解成「city」與「category」</p><p>1. 資料預處理</p><pre>shops[shops.shop_id == 46]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/315/1*6bG2dWXcItgp84pICOTBOw.png" /></figure><pre># 由於 Сергиев Посад是一個城市，為了後續的資料分割，故刪除中間的空格。<br>shops.loc[ shops.shop_name == &#39;Сергиев Посад ТЦ &quot;7Я&quot;&#39;,&quot;shop_name&quot; ] = &#39;СергиевПосад ТЦ &quot;7Я&quot;&#39;</pre><p>2. 將「shops」資料集拆解成「city」與「category」</p><pre>shops[&quot;city&quot;] = shops.shop_name.str.split(&quot; &quot;).map( lambda x: x[0] )<br>shops[&quot;category&quot;] = shops.shop_name.str.split(&quot; &quot;).map( lambda x: x[1] )</pre><p>3. 「shops」資料集中「city」欄位，將資料 “ !Якутск ” 改為 “ Якутск ”</p><pre>shops.loc[shops.city == &quot;!Якутск&quot;, &quot;city&quot;] = &quot;Якутск&quot;</pre><p>4. 「shops」資料集中「category」欄位，出現頻率較低者(設為小於4)放在 “ etc ”</p><pre># 新設立一個空清單，「shops」資料集中「category」欄位中的資料僅寫入出現頻率較高的資料(在此設為4)於新清單內<br>category = []<br>for cat in shops.category.unique():<br>    print(cat, len(shops[shops.category == cat]) )<br>    if len(shops[shops.category == cat]) &gt; 4:<br>        category.append(cat)</pre><pre># 將出現頻率較低者放入&quot;etc&quot;內<br>shops.category = shops.category.apply( lambda x: x if (x in category) else &quot;etc&quot; )</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/97/1*wjy_bC6oIu1jierVRZpabg.png" /><figcaption>「shops」資料集中「category」欄位中頻率高者</figcaption></figure><pre># 檢視「shops」資料集中前5筆資料<br>shops.head(5)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/569/1*hy8UpwY8fOsKPp-huAxhQw.png" /><figcaption>「shops」資料集中前5筆資料</figcaption></figure><p>⋆ 「cats」資料集「item_category_name」欄位拆解成「type_code」，以便後續編碼</p><p>1. 將「cats」資料集「item_category_name」欄位拆解成「type_code」</p><pre>cats[&quot;type_code&quot;] = cats.item_category_name.apply( lambda x: x.split(&quot; &quot;)[0] ).astype(str)</pre><p>2. 「cats」資料集中「type_code」欄位，出現頻率較低者(設為小於4)放在 “ etc ”</p><pre># 新設立一個空清單，「cats」資料集中「type_code」欄位中的資料僅寫入出現頻率較高的資料(在此設為4)於新清單內<br>category = []<br>for cat in cats.type_code.unique():<br>    print(cat, len(cats[cats.type_code == cat]))<br>    if len(cats[cats.type_code == cat]) &gt; 4: <br>        category.append( cat )</pre><pre># 將出現頻率較低者放入&quot;etc&quot;內<br>cats.type_code = cats.type_code.apply(lambda x: x if (x in category) else &quot;etc&quot;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/222/1*cycVS0sDYHw4AGEKb1i6Sg.png" /><figcaption>「cats」資料集中「type_code」欄位中頻率高者</figcaption></figure><pre># 檢視「cats」資料集中前5筆資料<br>cats.head(5)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/504/1*_KRP3hvguHVWvihR00FgPQ.png" /><figcaption>「cats」資料集中前5筆資料</figcaption></figure><p>⋆ 「items」資料集「item_name」欄位有 &quot; ! &quot;、&quot; *** &quot;、&#39;&#39; ( ) &#39;&#39;、&quot; [ ] &quot;等符號</p><pre># 檢視「items」資料集前10筆資料<br>items.head(10)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/649/1*xn-MIUQxfiPJz8VT9BtXvQ.png" /></figure><pre># 可以看見「item_name」欄位有 &quot; ! &quot;、&quot; *** &quot;、&quot;( )&quot;、&quot;[ ]&quot;等符號<br># 使用分割與替代函數切割名稱</pre><pre>items[&quot;name1&quot;], items[&quot;name2&quot;] = items.item_name.str.split(&quot;[&quot;, 1).str<br>items[&quot;name1&quot;], items[&quot;name3&quot;] = items.item_name.str.split(&quot;(&quot;, 1).str</pre><pre>items[&quot;name2&quot;] = items.name2.str.replace(&#39;[^A-Za-z0-9А-Яа-я]+&#39;, &quot; &quot;).str.lower()<br>items[&quot;name3&quot;] = items.name3.str.replace(&#39;[^A-Za-z0-9А-Яа-я]+&#39;, &quot; &quot;).str.lower()<br>items = items.fillna(&#39;0&#39;)</pre><pre># 定義函數<br>import re<br>def name_correction(x):<br>    x = x.lower()<br>    x = x.partition(&#39;[&#39;)[0]<br>    x = x.partition(&#39;(&#39;)[0]<br>    x = re.sub(&#39;[^A-Za-z0-9А-Яа-я]+&#39;, &#39; &#39;, x)<br>    x = x.replace(&#39;  &#39;, &#39; &#39;)<br>    x = x.strip()<br>    return x</pre><pre># 套用函數，並更改至「items」資料集的「item_name」欄位<br>items[&quot;item_name&quot;] = items[&quot;item_name&quot;].apply(lambda x: name_correction(x))<br>items.name2 = items.name2.apply( lambda x: x[:-1] if x !=&quot;0&quot; else &quot;0&quot;)</pre><pre># 檢視「items」資料集前5筆資料<br>items.head(5)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CfNZX7NEhvDqX87L1nFX3w.png" /></figure><blockquote>Step<strong> 2.5 資料編碼</strong></blockquote><p>⋆ 「shops」資料集</p><pre># 使用sklearn中的LabelEncoder進行「shops」資料集欄位的編碼<br>from sklearn.preprocessing import LabelEncoder<br>shops[&quot;shop_category&quot;] = LabelEncoder().fit_transform( shops.category )<br>shops[&quot;shop_city&quot;] = LabelEncoder().fit_transform( shops.city )</pre><pre># 使用編碼後的欄位，重新整合成「shops」資料集<br>shops = shops[[&quot;shop_id&quot;, &quot;shop_category&quot;, &quot;shop_city&quot;]]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/796/1*3VpaRCEq3C9MYY9Jc2T6Fg.png" /></figure><p>⋆ 「cats」資料集</p><p>1. 「cats」資料集「type_code」欄位編碼</p><pre># 使用sklearn中的LabelEncoder進行「cats」資料集「type_code」欄位的編碼<br>cats.type_code = LabelEncoder().fit_transform(cats.type_code)</pre><p>2. 「cats」資料集「item_category_name」欄位切割</p><pre># 檢視「cats」資料集中前5筆資料<br>cats.head(5)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/539/1*Vwr0RHhZZrOUAzUQakU_zw.png" /></figure><pre># 「cats」資料集「item_category_name」欄位切割<br>cats[&quot;split&quot;] = cats.item_category_name.apply(lambda x: x.split(&quot;-&quot;))<br>cats[&quot;subtype&quot;] = cats.split.apply(lambda x: x[1].strip() if len(x) &gt; 1 else x[0].strip())</pre><pre># 檢視「cats」資料集中前5筆資料<br>cats.head(5)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/907/1*uZcJRwAV_Pa5cfncVnoK5w.png" /></figure><p>3. 「cats」資料集「subtype」欄位編碼</p><pre># 使用sklearn中的LabelEncoder進行「cats」資料集「subtype」欄位編碼<br>cats[&quot;subtype_code&quot;] = LabelEncoder().fit_transform( cats[&quot;subtype&quot;] )</pre><pre># 合併編碼欄位<br>cats = cats[[&quot;item_category_id&quot;, &quot;subtype_code&quot;, &quot;type_code&quot;]]</pre><pre># 檢視「cats」資料集前5筆資料<br>cats.head(5)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/420/1*BVhVR6JHj1TeF_V_p-3PTg.png" /><figcaption>編碼後「cats」資料集前5筆資料</figcaption></figure><p>⋆ 「items」資料集</p><p>1. 新增「type」欄位，將常出現的品項列入其中</p><pre>items[&quot;type&quot;] = items.name2.apply(lambda x: x[0:8] if x.split(&quot; &quot;)[0] == &quot;xbox&quot; else x.split(&quot; &quot;)[0] )<br>items.loc[(items.type == &quot;x360&quot;) | (items.type == &quot;xbox360&quot;) | (items.type == &quot;xbox 360&quot;) ,&quot;type&quot;] = &quot;xbox 360&quot;<br>items.loc[ items.type == &quot;&quot;, &quot;type&quot;] = &quot;mac&quot;<br>items.type = items.type.apply( lambda x: x.replace(&quot; &quot;, &quot;&quot;) )<br>items.loc[ (items.type == &#39;pc&#39; )| (items.type == &#39;pс&#39;) | (items.type == &quot;pc&quot;), &quot;type&quot; ] = &quot;pc&quot;<br>items.loc[ items.type == &#39;рs3&#39; , &quot;type&quot;] = &quot;ps3&quot;</pre><pre># 檢視「items」資料集前5筆資料<br>items.head(5)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3NYNxb3-AUoIs13QqB_FOQ.png" /></figure><pre># 將「type」欄位依照 &quot;item_id&quot; 累積計數<br>group_sum = items.groupby([&quot;type&quot;]).agg({&quot;item_id&quot;:&quot;count&quot;})<br>group_sum = group_sum.reset_index()</pre><pre># 檢視 Dataframe「group_sum」<br>group_sum</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/134/1*PSzL8RMgzkFbivHOQB_9sQ.png" /></figure><p>2. 將Dataframe「group_sum」出現頻率較低(設為小於40)者列為 &quot;etc&quot;，並套用到「items」資料集「name2」欄位</p><pre># 新設立一個空清單，Dataframe「group_sum」中的資料寫入出現頻率較低的資料(在此設為40)於清單內<br>drop_cols = []<br>for cat in group_sum.type.unique():<br>    if group_sum.loc[(group_sum.type == cat), &quot;item_id&quot;].values[0] &lt;40:<br>        drop_cols.append(cat)</pre><pre># 套用至「items」資料集「name2」欄位<br>items.name2 = items.name2.apply( lambda x: &quot;etc&quot; if (x in drop_cols) else x )<br>items = items.drop([&quot;type&quot;], axis = 1)</pre><p>3. 「items」資料集「name2」欄位與「name3」欄位編碼</p><pre># 使用sklearn中的LabelEncoder進行「items」資料集「name2」欄位與「name3」欄位編碼<br>items.name2 = LabelEncoder().fit_transform(items.name2)<br>items.name3 = LabelEncoder().fit_transform(items.name3)</pre><pre>items.drop([&quot;item_name&quot;, &quot;name1&quot;],axis = 1, inplace= True)</pre><pre># 檢視「items」資料集<br>items.head()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/357/1*bOMCKe1QtI-PpV2mwwqTyA.png" /></figure><h3>Step 3 產生資料集</h3><blockquote>Step 3.1 從「train」資料集製作 Dataframe「matrix」</blockquote><pre># 從「train」資料集挑出不重複的「shop_id」與「item_id」並放入 Dataframe「matrix」<br>matrix = []<br>cols  = [&quot;date_block_num&quot;, &quot;shop_id&quot;, &quot;item_id&quot;]<br>for i in range(34):<br>    sales = train[train.date_block_num == i]<br>    matrix.append( np.array(list( product( [i], sales.shop_id.unique(), sales.item_id.unique() ) ), dtype = np.int16) )</pre><pre># Dataframe「matrix」外觀美化<br>matrix = pd.DataFrame( np.vstack(matrix), columns = cols )<br>matrix[&quot;date_block_num&quot;] = matrix[&quot;date_block_num&quot;].astype(np.int8)<br>matrix[&quot;shop_id&quot;] = matrix[&quot;shop_id&quot;].astype(np.int8)<br>matrix[&quot;item_id&quot;] = matrix[&quot;item_id&quot;].astype(np.int16)<br>matrix.sort_values( cols, inplace = True )</pre><pre># 檢視 Dataframe「matrix」<br>matrix</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/348/1*r_wviKevUxAQBJrCQmqFmA.png" /><figcaption>Dataframe「matrix」</figcaption></figure><blockquote>Step 3.2 Dataframe「matrix」新增欄位－「item_cnt_month」</blockquote><pre>group = train.groupby( [&quot;date_block_num&quot;, &quot;shop_id&quot;, &quot;item_id&quot;] ).agg( {&quot;item_cnt_day&quot;: [&quot;sum&quot;]} )<br>group.columns = [&quot;item_cnt_month&quot;]<br>group.reset_index( inplace = True)</pre><pre># 合併 Dataframe「matrix」與「group」，並覆蓋舊Dataframe「matrix」<br>matrix = pd.merge( matrix, group, on = cols, how = &quot;left&quot; )</pre><pre># Kaggle比賽要求(True target values are clipped into [0,20] range.)<br>matrix[&quot;item_cnt_month&quot;] = matrix[&quot;item_cnt_month&quot;].<br>fillna(0).clip(0,20).astype(np.float16)</pre><blockquote>Step 3.3 將「test」資料集合併至 Dataframe「matrix」，並填上0</blockquote><pre>test[&quot;date_block_num&quot;] = 34<br>test[&quot;date_block_num&quot;] = test[&quot;date_block_num&quot;].astype(np.int8)<br>test[&quot;shop_id&quot;] = test.shop_id.astype(np.int8)<br>test[&quot;item_id&quot;] = test.item_id.astype(np.int16)<br>test.head()</pre><pre># 使用concat函數合併<br>matrix = pd.concat([matrix, test.drop([&quot;ID&quot;],axis = 1)], ignore_index=True, sort=False, keys=cols)<br>matrix.fillna( 0, inplace = True )</pre><pre># 檢視 Dataframe「matrix」<br>matrix</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/488/1*UfJOKNFRHsv9hyBxH9BHvw.png" /></figure><blockquote>Step 3.4 將「shops」、「items」與「cats」資料集合併至 Dataframe「matrix」</blockquote><pre>matrix = pd.merge( matrix, shops, on = [&quot;shop_id&quot;], how = &quot;left&quot; )<br>matrix = pd.merge(matrix, items, on = [&quot;item_id&quot;], how = &quot;left&quot;)<br>matrix = pd.merge( matrix, cats, on = [&quot;item_category_id&quot;], how = &quot;left&quot; )</pre><pre># 壓縮檔案大小<br>matrix[&quot;shop_city&quot;] = matrix[&quot;shop_city&quot;].astype(np.int8)<br>matrix[&quot;shop_category&quot;] = matrix[&quot;shop_category&quot;].astype(np.int8)<br>matrix[&quot;item_category_id&quot;] = matrix[&quot;item_category_id&quot;].astype(np.int8)<br>matrix[&quot;subtype_code&quot;] = matrix[&quot;subtype_code&quot;].astype(np.int8)<br>matrix[&quot;name2&quot;] = matrix[&quot;name2&quot;].astype(np.int8)<br>matrix[&quot;name3&quot;] = matrix[&quot;name3&quot;].astype(np.int16)<br>matrix[&quot;type_code&quot;] = matrix[&quot;type_code&quot;].astype(np.int8)</pre><pre># 檢視 Dataframe「matrix」<br>matrix</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Cr5J689SJXSQArerduLYEA.png" /></figure><blockquote>Step 3.5 Dataframe「matrix」新增欄位－「item_cnt_month」落後3期</blockquote><pre># 定義落後期函數<br>def lag_feature( df,lags, cols ):<br>    for col in cols:<br>        print(col)<br>        tmp = df[[&quot;date_block_num&quot;, &quot;shop_id&quot;,&quot;item_id&quot;,col ]]<br>        for i in lags:<br>            shifted = tmp.copy()<br>            shifted.columns = [&quot;date_block_num&quot;, &quot;shop_id&quot;, &quot;item_id&quot;, col + &quot;_lag_&quot;+str(i)]<br>            shifted.date_block_num = shifted.date_block_num + i<br>            df = pd.merge(df, shifted, on=[&#39;date_block_num&#39;,&#39;shop_id&#39;,&#39;item_id&#39;], how=&#39;left&#39;)<br>    return df</pre><pre># Dataframe「matrix」新增「item_cnt_month_lag_1」、「item_cnt_month_lag_2」與「item_cnt_month_lag_3」欄位</pre><pre>matrix = lag_feature( matrix, [1,2,3], [&quot;item_cnt_month&quot;] )</pre><blockquote>Step 3.6 Dataframe「matrix」新增欄位－「date_avg_item_cnt」落後1期</blockquote><pre># 計算 Dataframe「matrix」的「item_cnt_month」的平均值，並以Dataframe「matrix」的「date_block_num」欄位合併</pre><pre>group = matrix.groupby( [&quot;date_block_num&quot;] ).agg({&quot;item_cnt_month&quot; : [&quot;mean&quot;]})<br>group.columns = [&quot;date_avg_item_cnt&quot;]<br>group.reset_index(inplace = True)</pre><pre># Dataframe「group」的「date_avg_item_cnt」欄位取1期落後期，並合併到Dataframe「matrix」，令新增欄位為「date_avg_item_cnt_lag_1」</pre><pre>matrix = pd.merge(matrix, group, on = [&quot;date_block_num&quot;], how = &quot;left&quot;)<br>matrix.date_avg_item_cnt = matrix[&quot;date_avg_item_cnt&quot;].astype(np.float16)<br>matrix = lag_feature( matrix, [1], [&quot;date_avg_item_cnt&quot;] )<br>matrix.drop( [&quot;date_avg_item_cnt&quot;], axis = 1, inplace = True )</pre><blockquote>Step 3.7 Dataframe「matrix」新增欄位－「date_item_avg_item_cnt」落後3期</blockquote><pre># 計算 Dataframe「matrix」的「item_cnt_month」的平均值，並以Dataframe「matrix」的「date_block_num」與「item_id」欄位合併</pre><pre>group = matrix.groupby([&#39;date_block_num&#39;, &#39;item_id&#39;]).agg({&#39;item_cnt_month&#39;: [&#39;mean&#39;]})<br>group.columns = [ &#39;date_item_avg_item_cnt&#39; ]<br>group.reset_index(inplace=True)</pre><pre># Dataframe「group」的「date_avg_item_cnt」欄位取3期落後期，並合併到Dataframe「matrix」，令新增欄位為「date_item_avg_item_cnt_lag_1」、「date_item_avg_item_cnt_lag_2」與「date_item_avg_item_cnt_lag_3」</pre><pre>matrix = pd.merge(matrix, group, on=[&#39;date_block_num&#39;,&#39;item_id&#39;], how=&#39;left&#39;)<br>matrix.date_item_avg_item_cnt = matrix[&#39;date_item_avg_item_cnt&#39;].astype(np.float16)<br>matrix = lag_feature(matrix, [1,2,3], [&#39;date_item_avg_item_cnt&#39;])<br>matrix.drop([&#39;date_item_avg_item_cnt&#39;], axis=1, inplace=True)</pre><blockquote>Step 3.8 Dataframe「matrix」新增欄位－「date_shop_avg_item_cnt」落後3期</blockquote><pre># 計算 Dataframe「matrix」的「item_cnt_month」的平均值，並以Dataframe「matrix」的「date_block_num」與「shop_id」欄位合併</pre><pre>group = matrix.groupby( [&quot;date_block_num&quot;,&quot;shop_id&quot;] ).agg({&quot;item_cnt_month&quot; : [&quot;mean&quot;]})<br>group.columns = [&quot;date_shop_avg_item_cnt&quot;]<br>group.reset_index(inplace = True)</pre><pre># Dataframe「group」的「date_shop_avg_item_cnt」欄位取3期落後期，並合併到Dataframe「matrix」，令新增欄位為「date_shop_avg_item_cnt_lag_1」、「date_shop_avg_item_cnt_lag_2」與「date_shop_avg_item_cnt_lag_3」欄位</pre><pre>matrix = pd.merge(matrix, group, on = [&quot;date_block_num&quot;,&quot;shop_id&quot;], how = &quot;left&quot;)<br>matrix.date_avg_item_cnt = matrix[&quot;date_shop_avg_item_cnt&quot;].astype(np.float16)<br>matrix = lag_feature( matrix, [1,2,3], [&quot;date_shop_avg_item_cnt&quot;] )<br>matrix.drop( [&quot;date_shop_avg_item_cnt&quot;], axis = 1, inplace = True )</pre><blockquote>Step 3.9 Dataframe「matrix」新增欄位－「date_shop_item_avg_item_cnt」落後3期</blockquote><pre># 計算 Dataframe「matrix」的「item_cnt_month」的平均值，並以Dataframe「matrix」的「date_block_num」、「shop_id」與「item_id」欄位合併</pre><pre>group = matrix.groupby( [&quot;date_block_num&quot;,&quot;shop_id&quot;,&quot;item_id&quot;] ).agg({&quot;item_cnt_month&quot; : [&quot;mean&quot;]})<br>group.columns = [&quot;date_shop_item_avg_item_cnt&quot;]<br>group.reset_index(inplace = True)</pre><pre># Dataframe「group」的「date_shop_item_avg_item_cnt」欄位取3期落後期，並合併到Dataframe「matrix」，令新增欄位為「date_shop_item_avg_item_cnt_lag_1」、「date_shop_item_avg_item_cnt_lag_2」與「date_shop_item_avg_item_cnt_lag_3」欄位</pre><pre>matrix = pd.merge(matrix, group, on = [&quot;date_block_num&quot;,&quot;shop_id&quot;,&quot;item_id&quot;], how = &quot;left&quot;)<br>matrix.date_avg_item_cnt = matrix[&quot;date_shop_item_avg_item_cnt&quot;].astype(np.float16)<br>matrix = lag_feature( matrix, [1,2,3], [&quot;date_shop_item_avg_item_cnt&quot;] )<br>matrix.drop( [&quot;date_shop_item_avg_item_cnt&quot;], axis = 1, inplace = True )</pre><blockquote>Step 3.10 Dataframe「matrix」新增欄位－「date_shop_subtype_avg_item_cnt」落後1期</blockquote><pre># 計算 Dataframe「matrix」的「item_cnt_month」的平均值，並以Dataframe「matrix」的「date_block_num」、「shop_id」與「subtype_code」欄位合併</pre><pre>group = matrix.groupby([&#39;date_block_num&#39;, &#39;shop_id&#39;, &#39;subtype_code&#39;]).agg({&#39;item_cnt_month&#39;: [&#39;mean&#39;]})<br>group.columns = [&#39;date_shop_subtype_avg_item_cnt&#39;]<br>group.reset_index(inplace=True)</pre><pre># Dataframe「group」的「date_shop_subtype_avg_item_cnt」欄位取1期落後期，並合併到Dataframe「matrix」，令新增欄位為「date_shop_subtype_avg_item_cnt_lag_1」</pre><pre>matrix = pd.merge(matrix, group, on=[&#39;date_block_num&#39;, &#39;shop_id&#39;, &#39;subtype_code&#39;], how=&#39;left&#39;)<br>matrix.date_shop_subtype_avg_item_cnt = matrix[&#39;date_shop_subtype_avg_item_cnt&#39;].astype(np.float16)<br>matrix = lag_feature(matrix, [1], [&#39;date_shop_subtype_avg_item_cnt&#39;])<br>matrix.drop([&#39;date_shop_subtype_avg_item_cnt&#39;], axis=1, inplace=True)</pre><blockquote>Step 3.11 Dataframe「matrix」新增欄位－「date_city_avg_item_cnt」落後1期</blockquote><pre># 計算 Dataframe「matrix」的「item_cnt_month」的平均值，並以Dataframe「matrix」的「date_block_num」與「shop_city」欄位合併</pre><pre>group = matrix.groupby([&#39;date_block_num&#39;, &#39;shop_city&#39;]).agg({&#39;item_cnt_month&#39;: [&#39;mean&#39;]})<br>group.columns = [&#39;date_city_avg_item_cnt&#39;]<br>group.reset_index(inplace=True)</pre><pre># Dataframe「group」的「date_city_avg_item_cnt」欄位取1期落後期，並合併到Dataframe「matrix」，令新增欄位為「date_city_avg_item_cnt_lag_1」</pre><pre>matrix = pd.merge(matrix, group, on=[&#39;date_block_num&#39;, &quot;shop_city&quot;], how=&#39;left&#39;)<br>matrix.date_city_avg_item_cnt = matrix[&#39;date_city_avg_item_cnt&#39;].astype(np.float16)<br>matrix = lag_feature(matrix, [1], [&#39;date_city_avg_item_cnt&#39;])<br>matrix.drop([&#39;date_city_avg_item_cnt&#39;], axis=1, inplace=True)</pre><blockquote>Step 3.12 Dataframe「matrix」新增欄位－「date_item_city_avg_item_cnt」落後1期</blockquote><pre># 計算 Dataframe「matrix」的「item_cnt_month」的平均值，並以Dataframe「matrix」的「date_block_num」、「item_id」與「shop_city」欄位合併</pre><pre>group = matrix.groupby([&#39;date_block_num&#39;, &#39;item_id&#39;, &#39;shop_city&#39;]).agg({&#39;item_cnt_month&#39;: [&#39;mean&#39;]})<br>group.columns = [ &#39;date_item_city_avg_item_cnt&#39; ]<br>group.reset_index(inplace=True)</pre><pre># Dataframe「group」的「date_item_city_avg_item_cnt」欄位取1期落後期，並合併到Dataframe「matrix」，令新增欄位為「date_item_city_avg_item_cnt_lag_1」</pre><pre>matrix = pd.merge(matrix, group, on=[&#39;date_block_num&#39;, &#39;item_id&#39;, &#39;shop_city&#39;], how=&#39;left&#39;)<br>matrix.date_item_city_avg_item_cnt = matrix[&#39;date_item_city_avg_item_cnt&#39;].astype(np.float16)<br>matrix = lag_feature(matrix, [1], [&#39;date_item_city_avg_item_cnt&#39;])<br>matrix.drop([&#39;date_item_city_avg_item_cnt&#39;], axis=1, inplace=True)</pre><blockquote>Step 3.13 Dataframe「matrix」新增欄位－「delta_price_lag」</blockquote><pre># 新增「item_avg_item_price」欄位<br>group = train.groupby( [&quot;item_id&quot;] ).agg({&quot;item_price&quot;: [&quot;mean&quot;]})<br>group.columns = [&quot;item_avg_item_price&quot;]<br>group.reset_index(inplace = True)<br>matrix = matrix.merge( group, on = [&quot;item_id&quot;], how = &quot;left&quot; )<br>matrix[&quot;item_avg_item_price&quot;] = matrix[&quot;item_avg_item_price&quot;].astype(np.float16)</pre><pre># 新增「date_item_avg_item_price」欄位<br>group = train.groupby( [&quot;date_block_num&quot;,&quot;item_id&quot;] ).agg( {&quot;item_price&quot;: [&quot;mean&quot;]} )<br>group.columns = [&quot;date_item_avg_item_price&quot;]<br>group.reset_index(inplace = True)<br>matrix = matrix.merge(group, on = [&quot;date_block_num&quot;,&quot;item_id&quot;], how = &quot;left&quot;)<br>matrix[&quot;date_item_avg_item_price&quot;] = matrix[&quot;date_item_avg_item_price&quot;].astype(np.float16)</pre><pre># 新增「delta_price_lag」欄位<br>lags = [1, 2, 3]<br>matrix = lag_feature( matrix, lags, [&quot;date_item_avg_item_price&quot;] )<br>for i in lags:<br>    matrix[&quot;delta_price_lag_&quot; + str(i) ] = (matrix[&quot;date_item_avg_item_price_lag_&quot; + str(i)]- matrix[&quot;item_avg_item_price&quot;] )/ matrix[&quot;item_avg_item_price&quot;]</pre><pre>def select_trends(row) :<br>    for i in lags:<br>        if row[&quot;delta_price_lag_&quot; + str(i)]:<br>            return row[&quot;delta_price_lag_&quot; + str(i)]<br>    return 0</pre><pre>matrix[&quot;delta_price_lag&quot;] = matrix.apply(select_trends, axis = 1)<br>matrix[&quot;delta_price_lag&quot;] = matrix.delta_price_lag.astype( np.float16 )<br>matrix[&quot;delta_price_lag&quot;].fillna( 0 ,inplace = True)</pre><pre>features_to_drop = [&quot;item_avg_item_price&quot;, &quot;date_item_avg_item_price&quot;]<br>for i in lags:<br>    features_to_drop.append(&quot;date_item_avg_item_price_lag_&quot; + str(i) )<br>    features_to_drop.append(&quot;delta_price_lag_&quot; + str(i) )<br>matrix.drop(features_to_drop, axis = 1, inplace = True)</pre><blockquote>Step 3.14 Dataframe「matrix」新增欄位－「delta_revenue」落後1期</blockquote><pre># 新增「train」資料集「revenue」欄位<br>train[&quot;revenue&quot;] = train[&quot;item_cnt_day&quot;] * train[&quot;item_price&quot;]</pre><pre>group = train.groupby( [&quot;date_block_num&quot;,&quot;shop_id&quot;] ).agg({&quot;revenue&quot;: [&quot;sum&quot;] })<br>group.columns = [&quot;date_shop_revenue&quot;]<br>group.reset_index(inplace = True)</pre><pre>matrix = matrix.merge( group , on = [&quot;date_block_num&quot;, &quot;shop_id&quot;], how = &quot;left&quot; )<br>matrix[&#39;date_shop_revenue&#39;] = matrix[&#39;date_shop_revenue&#39;].astype(np.float32)</pre><pre>group = group.groupby([&quot;shop_id&quot;]).agg({ &quot;date_block_num&quot;:[&quot;mean&quot;] })<br>group.columns = [&quot;shop_avg_revenue&quot;]<br>group.reset_index(inplace = True )</pre><pre>matrix = matrix.merge( group, on = [&quot;shop_id&quot;], how = &quot;left&quot; )<br>matrix[&quot;shop_avg_revenue&quot;] = matrix[&quot;shop_avg_revenue&quot;].astype(np.float32)<br>matrix[&quot;delta_revenue&quot;] = (matrix[&#39;date_shop_revenue&#39;] - matrix[&#39;shop_avg_revenue&#39;]) / matrix[&#39;shop_avg_revenue&#39;]<br>matrix[&quot;delta_revenue&quot;] = matrix[&quot;delta_revenue&quot;]. astype(np.float32)</pre><pre>matrix = lag_feature(matrix, [1], [&quot;delta_revenue&quot;])<br>matrix[&quot;delta_revenue_lag_1&quot;] = matrix[&quot;delta_revenue_lag_1&quot;].astype(np.float32)<br>matrix.drop( [&quot;date_shop_revenue&quot;, &quot;shop_avg_revenue&quot;, &quot;delta_revenue&quot;] ,axis = 1, inplace = True)</pre><pre># 檢視 Dataframe「matrix」<br>matrix.head().T</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/708/1*QQVL0Fqmla0wVEwWDUZRnw.png" /></figure><blockquote>Step 3.15 Dataframe「matrix」新增欄位－「days」、「item_shop_first_sale」與「item_first_sale」</blockquote><pre># 將日期從連續數值改成1-12月<br>matrix[&quot;month&quot;] = matrix[&quot;date_block_num&quot;] % 12<br>days = pd.Series([31,28,31,30,31,30,31,31,30,31,30,31])<br>matrix[&quot;days&quot;] = matrix[&quot;month&quot;].map(days).astype(np.int8)</pre><pre># 第一個月<br>matrix[&quot;item_shop_first_sale&quot;] = matrix[&quot;date_block_num&quot;] - matrix.groupby([&quot;item_id&quot;,&quot;shop_id&quot;])[&quot;date_block_num&quot;].transform(&#39;min&#39;)<br>matrix[&quot;item_first_sale&quot;] = matrix[&quot;date_block_num&quot;] - matrix.groupby([&quot;item_id&quot;])[&quot;date_block_num&quot;].transform(&#39;min&#39;)</pre><pre>matrix = matrix[matrix[&quot;date_block_num&quot;] &gt; 3]</pre><h3>Step 4 產生訓練與測試資料集</h3><pre># 將方才產生的 Dataframe「matrix」複製到「data」<br>data = matrix.copy()</pre><pre># 產生訓練與驗證資料集<br>X_train = data[data.date_block_num &lt; 33].drop([&#39;item_cnt_month&#39;], axis=1)<br>Y_train = data[data.date_block_num &lt; 33][&#39;item_cnt_month&#39;]<br>X_valid = data[data.date_block_num == 33].drop([&#39;item_cnt_month&#39;], axis=1)<br>Y_valid = data[data.date_block_num == 33][&#39;item_cnt_month&#39;]<br>X_test = data[data.date_block_num == 34].drop([&#39;item_cnt_month&#39;], axis=1)</pre><h3>Step 5 建立模型與預測</h3><pre>from xgboost import XGBRegressor</pre><pre>model = XGBRegressor(<br>    max_depth=10,<br>    n_estimators=1000,<br>    min_child_weight=0.5, <br>    colsample_bytree=0.8, <br>    subsample=0.8, <br>    eta=0.1,<br>    seed=42)</pre><pre>train_history=model.fit(<br>    X_train, <br>    Y_train, <br>    eval_metric=&quot;rmse&quot;, <br>    eval_set=[(X_train, Y_train), (X_valid, Y_valid)],<br>    verbose=True, <br>    early_stopping_rounds = 20)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h9O8iukNUpFn4oPC614X2g.png" /></figure><pre># 檢視<br>train_history</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/666/1*h6O0FJkgnIpH8ud210jIOQ.png" /></figure><pre># 預測<br>Y_test = model.predict(X_test).clip(0, 20)</pre><pre># 將預測結果輸出成csv檔案<br>submission = pd.DataFrame({<br>    &quot;ID&quot;: test.index, <br>    &quot;item_cnt_month&quot;: Y_test<br>})<br>submission.to_csv(&#39;xgb_submission.csv&#39;, index=False)</pre><blockquote><strong><em>附圖 : 在Kaggle上的成績</em></strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BuYtosrep4oWAMDyH_HGbw.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8a15f852e874" width="1" height="1" alt=""><hr><p><a href="https://medium.com/yuying%E7%9A%84%E4%BD%9C%E5%93%81%E9%9B%86/python-%E7%94%A2%E5%93%81%E9%8A%B7%E5%94%AE%E9%87%8F%E9%A0%90%E6%B8%AC-with-code-kaggle-top-6-8a15f852e874">[Python] 產品銷售量預測 with code (Kaggle Top 6%)</a> was originally published in <a href="https://medium.com/yuying%E7%9A%84%E4%BD%9C%E5%93%81%E9%9B%86">Works</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[R] 預測紐約家庭收入大於150K的影響變數有哪些]]></title>
            <link>https://medium.com/yuying%E7%9A%84%E4%BD%9C%E5%93%81%E9%9B%86/r-%E9%A0%90%E6%B8%AC%E7%B4%90%E7%B4%84%E5%AE%B6%E5%BA%AD%E6%94%B6%E5%85%A5%E5%A4%A7%E6%96%BC150k%E7%9A%84%E5%BD%B1%E9%9F%BF%E8%AE%8A%E6%95%B8%E6%9C%89%E5%93%AA%E4%BA%9B-d612aa56cf96?source=rss-65a785c4c90a------2</link>
            <guid isPermaLink="false">https://medium.com/p/d612aa56cf96</guid>
            <category><![CDATA[r]]></category>
            <dc:creator><![CDATA[Airater]]></dc:creator>
            <pubDate>Sat, 18 Jul 2020 15:05:02 GMT</pubDate>
            <atom:updated>2020-07-18T15:09:06.200Z</atom:updated>
            <content:encoded><![CDATA[<p><em>本文使用R進行邏輯斯迴歸分析。</em></p><blockquote><strong><em>1. 研究動機</em></strong></blockquote><p>已知有眾多會影響家計單位所得的因素，本文以問卷調查的方式在美國紐約州進行資料蒐集，透過受訪者的回答探討哪些是影響預測所得是否大於150K的變數，進而得知針對哪方面制定政策與改進，會對所得的提升有所幫助。</p><blockquote><strong><em>2. 分析問題與資料說明</em></strong></blockquote><p>本文欲分析預測美國紐約家庭收入大於$15,000的影響特徵因素有哪些，故目標變數為大於$15,000(above150K)。</p><p>根據調查對象回答之家庭類型(FamilyType)、家庭收入(FamilyIncome)、臥室數量(NumBedrooms)、小孩數量(NumChildren)、家戶人數(NumPeople)、房間數量(NumRooms)、房屋類型(NumUnits)、汽車數量(NumVehicles)、工作人數(NumWorkers)、擁有房屋方式(OwnRent)、屋齡(YearBuilt)、房屋費用(HouseCosts)、電費帳單(ElectricBill)、糧票(FoodStamp)、加熱燃料(HeatingFuel)、保險費用(Insurance)、使用語言(Language)，從中挑選出最具影響力的變數，並使用邏輯斯迴歸建立模型。</p><blockquote><strong><em>3. 資料來源</em></strong></blockquote><p>本文資料取自於Jared Lander個人網站，資料實證區間為2010年，調查對象為美國紐約居民，約有兩萬筆資料。</p><blockquote><strong><em>4. 實證結果</em></strong></blockquote><h3><strong>4.1 檢視資料</strong></h3><p>首先，先檢視連續變數的敘述統計，如圖1所示。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U8DxWBSroTIH92vS62arRw.png" /><figcaption>圖1 連續變數的敘述統計</figcaption></figure><p>為了後續進行分析，故將家庭收入(FamilyIncome)變數以$150K為分界，分為家庭收入大於等於$150K(&gt; =150K)及家庭收入小於$150K(&lt;150K)。</p><h3><strong>4.2 資料探勘(EDA)</strong></h3><p><strong>▶ 目標變數 : 家庭收入(FamilyIncome)</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*U7k3X48pPSjsla-3VWZLNg.png" /><figcaption>圖2 家庭收入之機率密度分配圖</figcaption></figure><p>▶ <strong>類別變數</strong></p><p>⑴ 家庭類型(FamilyType)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*krPLsJy8hakZaxCuFbZwhA.png" /><figcaption>圖3 家庭類型(FamilyType)</figcaption></figure><p>⑵ 房屋類型(NumUnits)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*I5hzEAqFhtyWaVxtSdwzXg.png" /><figcaption>圖4 房屋類型(NumUnits)</figcaption></figure><p>⑶ 擁有房屋方式(OwnRent)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*xN2Lv1BLH0K9IrgnQ9gG-A.png" /><figcaption>圖5 擁有房屋方式(OwnRent)</figcaption></figure><p>⑷ 屋齡(YearBuilt)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*09pgZr3ITpPq8Flot4Xg7g.png" /><figcaption>圖6 屋齡(YearBuilt)</figcaption></figure><p>⑸ 糧票(FoodStamp)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*E8WEyYzOOulK-eXiSzk7rg.png" /><figcaption>圖7 糧票(FoodStamp)</figcaption></figure><p>⑹ 加熱燃料(HeatingFuel)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*B5qqOnmlecjg_vALG6ZLCg.png" /><figcaption>圖8 加熱燃料(HeatingFuel)</figcaption></figure><p>⑺ 使用語言(Language)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*WCfmdxuoNAnForVhYnB0ZQ.png" /><figcaption>圖9 使用語言(Language)</figcaption></figure><p>▶ <strong>連續變數</strong></p><p>⑴ 臥室數量(NumBedrooms)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*e1guHvE2dFdc2hHVOTd8Gg.png" /><figcaption>圖10 臥室數量(NumBedrooms)</figcaption></figure><p>⑵ 小孩數量(NumChildren)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*GPd3hnN1NE_lkHYqhIuDGw.png" /><figcaption>圖11 小孩數量(NumChildren)</figcaption></figure><p>⑶ 家戶人數(NumPeople)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*0XTta-w4GuIw_irPKsfyGw.png" /><figcaption>圖12 家戶人數(NumPeople)</figcaption></figure><p>⑷ 房間數量(NumRooms)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*KG4mNa0kWvVDVbs3G28YPQ.png" /><figcaption>圖13 房間數量(NumRooms)</figcaption></figure><p>⑸ 汽車數量(NumVehicles)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*cZUOIWe-yTUecbVuWQMo5g.png" /><figcaption>圖14 汽車數量(NumVehicles)</figcaption></figure><p>⑹ 工作人數(NumWorkers)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*aVgRlHiKGnfnmMlbV1nmPQ.png" /><figcaption>圖15 工作人數(NumWorkers)</figcaption></figure><p>⑺ 房屋費用(HouseCosts)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rtuLII6LPvfC5VHn3vaz3A.png" /><figcaption>圖16 房屋費用(HouseCosts)</figcaption></figure><p>⑻ 電費帳單(ElectricBill)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*oO7LuPhNEpdMTIP7BBp8XQ.png" /><figcaption>圖17 電費帳單(ElectricBill)</figcaption></figure><p>⑼ 保險費用(Insurance)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*g-BTYdhYRg5rmvbRJlG1qQ.png" /><figcaption>圖18 保險費用(Insurance)</figcaption></figure><h3><strong>4.3 篩選變數</strong></h3><p>藉由計算各變數的IV值(Information Value)篩選出欲放入迴歸式的變數，結果如表1所示。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/234/1*FyYALSdNGSFPAtF7KbcRcQ.png" /><figcaption>表1 各變數IV值</figcaption></figure><p>已知IV值小於0.03表示變數無解釋能力；IV值介於0.03至0.09表示變數具有低解釋能力；IV值介於0.1至0.29表示變數具有中等解釋能力；IV值介於0.3至0.49表示變數具有高解釋能力；IV值大於0.5表示變數具有極高的解釋能力。</p><p>故由表1可知應該留下IV值大於0.3者的變數－房屋費用(HouseCosts)、保險費用(Insurance)、房間數量(NumRooms)、臥室數量(NumBedrooms)。</p><h3><strong>4.4 塑模與預測</strong></h3><p>本文使用邏輯斯迴歸建立模型，邏輯斯迴歸方程式如下式表示。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/710/1*bo3-Zy8qWVFqPhHUL2g1hQ.png" /></figure><p>邏輯反函數如式(2)所示。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/674/1*GeXgcDN2yKevToVrKJ_ysQ.png" /></figure><p>使用R進行邏輯斯迴歸結果如圖19所示。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/705/1*XOT-oGdtdGM6KQbPYeKILA.png" /><figcaption>圖19 邏輯斯迴歸結果</figcaption></figure><p>由圖19可知，在顯著水準為0.05之下，各變數皆達統計上顯著。</p><p>緊接著藉由計算VIF值，判斷模型殘差是否存在共線性，結果如圖20所示。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/499/1*htlUxxchlXL7BPtBHeuXwQ.png" /><figcaption>圖20 變數VIF值</figcaption></figure><p>由於各變數VIF值皆未大於5，故模型殘差不存在共線性。</p><p>使用該模型對測試資料集進行預測，並繪製出ROC曲線，結果如圖21所示，在ROC曲線下面積為82.49%。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*bQqnNjELoisBMcl9fHENXA.png" /><figcaption>圖21 ROC曲線</figcaption></figure><p>此外，經計算後得出模型錯誤率為17.16%。</p><p>故綜合以上可得知此模型預測能力還算不錯。</p><h3><strong>4.5 模型診斷</strong></h3><p>使用逐步向前變數選取(Stepwise Variable Selection)，檢測是否需再挑選一次模型變數。</p><p>逐步向後(backward selection)的結果如圖22所示。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/701/1*GLd2mPHb6Xj9TO47w7BBEg.png" /><figcaption>圖22 逐步向後(backward selection)</figcaption></figure><p>逐步向前法(forward selection)的結果如圖23所示。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/704/1*_OEzYx3_wYMU2HVFShYlIA.png" /><figcaption>圖23 逐步向前(forward selection)</figcaption></figure><p>由圖19、圖22與圖23可知，原始模型、逐步向後與逐步向前的AIC值皆為相同，故使用原始模型即可。</p><h3><strong>4.6 結論</strong></h3><p>房屋費用(HouseCosts)、保險費用(Insurance)、房間數量(NumRooms)與臥室數量(NumBedrooms)為預測紐約家庭收入大於150K的主要影響變數，故若要針對提高整體紐約居民的所得時，對於房屋與保險相關政策的改進及調整將會有所幫助。</p><blockquote><strong><em>附錄 : R Code</em></strong></blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/38122ec74e701f125d1197f27647d440/href">https://medium.com/media/38122ec74e701f125d1197f27647d440/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d612aa56cf96" width="1" height="1" alt=""><hr><p><a href="https://medium.com/yuying%E7%9A%84%E4%BD%9C%E5%93%81%E9%9B%86/r-%E9%A0%90%E6%B8%AC%E7%B4%90%E7%B4%84%E5%AE%B6%E5%BA%AD%E6%94%B6%E5%85%A5%E5%A4%A7%E6%96%BC150k%E7%9A%84%E5%BD%B1%E9%9F%BF%E8%AE%8A%E6%95%B8%E6%9C%89%E5%93%AA%E4%BA%9B-d612aa56cf96">[R] 預測紐約家庭收入大於150K的影響變數有哪些</a> was originally published in <a href="https://medium.com/yuying%E7%9A%84%E4%BD%9C%E5%93%81%E9%9B%86">Works</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[STATA] 美國通膨率與長期利率趨勢分析]]></title>
            <link>https://medium.com/yuying%E7%9A%84%E4%BD%9C%E5%93%81%E9%9B%86/stata-%E7%BE%8E%E5%9C%8B%E9%80%9A%E8%86%A8%E7%8E%87%E8%88%87%E9%95%B7%E6%9C%9F%E5%88%A9%E7%8E%87%E8%B6%A8%E5%8B%A2%E5%88%86%E6%9E%90-a80e2e9979f0?source=rss-65a785c4c90a------2</link>
            <guid isPermaLink="false">https://medium.com/p/a80e2e9979f0</guid>
            <category><![CDATA[tata]]></category>
            <dc:creator><![CDATA[Airater]]></dc:creator>
            <pubDate>Mon, 06 Jul 2020 16:08:29 GMT</pubDate>
            <atom:updated>2020-07-29T12:09:37.648Z</atom:updated>
            <content:encoded><![CDATA[<h3>[Stata] 美國通膨率與長期利率趨勢分析 with code</h3><p>本文使用Stata進行VAR分析。</p><blockquote><strong><em>1. 摘要</em></strong></blockquote><p>在此以列點方式呈現本文分析步驟與得到的結論。</p><p><strong>⑴</strong> 確認兩時間序列不存在共整合關係後，以VAR方式建立模型。</p><p><strong>⑵</strong> 檢定模型殘差項發現為非常態且無自我相關。</p><p><strong>⑶</strong> 藉由繪製特徵圖得知模型係數具有穩定性。</p><p><strong>⑷</strong> 進行Granger因果檢定知悉序列間不存在Granger因果關係。</p><p><strong>⑸</strong> 衝擊反應(irf)分析知曉以下事實 :</p><p>🅐 長期利率會短期且來回衝擊通貨膨脹率，以前3期升幅最大，第4至第7期微幅震盪，第8期至第50期引響力大幅度增加，第50期後逐漸趨近於零，最後引響力近乎於零。</p><p>🅑 通貨膨脹率會短期且來回衝擊長期利率，以前3期降幅最大，第4至第7期微幅震盪，第8至第50期後引響力小幅度增加，第50期後逐漸趨近於零，最後引響力近乎於零。</p><p>⑹ 模型預測誤差變異分解得出，長期利率受過去自身影響大，而隨著期數拉長，通貨膨脹率受過去自身影響將會有明顯幅度的下降。</p><blockquote><strong><em>2. 研究方法</em></strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*75w2B-ysqzbi5lX7V0YuJQ.png" /></figure><blockquote><strong><em>3. 資料來源</em></strong></blockquote><p>本文實證期間為西元1956年第一季至2020年第一季，資料頻率為季。長期利率(Long-term interest rates)，取自於OECD；通貨膨脹率係以GDP deflator衡量，取自於經濟分析局(Bureau of Economic Analysis)。</p><blockquote><strong><em>4. 實證結果</em></strong></blockquote><p><strong>4.1 檢視資料</strong></p><p>首先，先檢視各變數的敘述統計，如圖1所示。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/581/1*130urRHlorAqT_Cr-Dlj6g.png" /><figcaption>圖1 敘述統計</figcaption></figure><p>將兩變數繪製成線圖，如圖2與圖3所示。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/1*0yMfsI2gk1JCBJzS0kzYzg.png" /><figcaption>圖2 長期利率</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/1*yGNReuyLhhV-ckeyZVCY7w.png" /><figcaption>圖3 通貨膨脹率</figcaption></figure><p>若將兩時間序列繪製在同一張圖內，結果如圖4所示。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/1*qqOwkgVVth8L32D7Jaz7nA.png" /><figcaption>圖4 長期利率與通貨膨脹率</figcaption></figure><p><strong>4.2 常態檢定</strong></p><p>由於共整合檢定要求時間序列須為常態，故分別對序列使用Jarque-Bera常態性檢定，其結果如圖5及圖6所示。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/415/1*AK7nfpAJYFT9GEuSYn-VhQ.png" /><figcaption>圖5 長期利率之Jarque-Bera常態性檢定</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/420/1*5eHoqZrGNKlaoGIp6z5EDQ.png" /><figcaption>圖6 通貨膨脹率之Jarque-Bera常態性檢定</figcaption></figure><p>在顯著水準0.05之下，兩變數皆拒絕虛無假設，故為非定態。</p><p>由於變數未達定態，因此取Natural log作為轉換，再對轉換後的變數進行Jarque-Bera常態性檢定，其結果如圖7及圖8所示。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/411/1*dLx2cAdvBJAJzljblsCwgA.png" /><figcaption>圖7 對數長期利率之Jarque-Bera常態性檢定</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/405/1*WW_yZqLO1kdfDOFj6uqWwg.png" /><figcaption>圖8 對數通貨膨脹率之Jarque-Bera常態性檢定</figcaption></figure><p>在顯著水準0.05之下，兩變數皆無法拒絕虛無假設，故為定態。</p><p><strong>4.3 單根檢定</strong></p><p>已知一個隨機漫步模型可分為三種情況－不含截距和趨勢、僅含截距、含截距和趨勢，分別對此三種情況做 ADF 單根檢定，虛無假設皆為存在單根，亦即時間序列非定態。</p><p>對數長期利率之ADF單根檢定結果如圖9、圖10及圖11所示。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/1*9unSJpHdm1ZrEIBPWMH1Yg.png" /><figcaption>圖9 對數長期利率－不含截距和時間趨勢的隨機漫步模型之ADF檢定結果</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/1*dkPQ1GBBYOLQTICrUd1pEw.png" /><figcaption>圖10 對數長期利率－僅含截距的隨機漫步模型之ADF檢定結果</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/1*aF7FbFNkpdTWgSSe0901MA.png" /><figcaption>圖11 對數長期利率－含截距和時間趨勢的隨機漫步模型之ADF檢定結果</figcaption></figure><p>在顯著水準0.05之下，圖9檢定統計量-0.606大於臨界值-1.950，無法拒絕虛無假設，時間序列為非定態；圖10檢定統計量-0.228大於臨界值-2.880，無法拒絕虛無假設，時間序列為非定態；圖11檢定統計量-1.315大於臨界值-3.430，無法拒絕虛無假設，時間序列為非定態。</p><p>由於對數長期利率未達定態，故取一階差分，再進行ADF單根檢定，結果如圖12、圖13與圖14所示。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/1*OkH7YfW0rYKka9Iu7M_oaA.png" /><figcaption>圖12 一階差分對數長期利率－不含截距和時間趨勢的隨機漫步模型之ADF檢定結果</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/1*82jWXTiQtPZWtNq7Fx5QeA.png" /><figcaption>圖13 一階差分對數長期利率－僅含截距的隨機漫步模型之ADF檢定結果</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/1*I38cNW-5Wfq-wxZKdDw9-w.png" /><figcaption>圖14 一階差分對數長期利率－含截距和時間趨勢的隨機漫步模型之ADF檢定結果</figcaption></figure><p>在顯著水準0.05之下，圖9檢定統計量-12.263小於臨界值-1.950，拒絕虛無假設，時間序列為定態；圖10檢定統計量-12.255小於臨界值-2.880，拒絕虛無假設，時間序列為定態；圖11檢定統計量-12.491小於臨界值-3.430，拒絕虛無假設，時間序列為定態。</p><p>對數通貨膨脹率之ADF單根檢定結果如圖15、圖16及圖17所示。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/1*BkjpaIqV1mkp7vCg5dc2oQ.png" /><figcaption>圖15 對數通貨膨脹率－不含截距和時間趨勢的隨機漫步模型之ADF檢定結果</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/1*PO0m-vzgeB59zO7YhrosRA.png" /><figcaption>圖16 對數通貨膨脹率－僅含截距的隨機漫步模型之ADF檢定結果</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/1*rAFsejAJi5nB2ru6dId6eQ.png" /><figcaption>圖17 對數通貨膨脹率－含截距和時間趨勢的隨機漫步模型之ADF檢定結果</figcaption></figure><p>在顯著水準0.05之下，圖15檢定統計量-3.494小於臨界值-1.950，拒絕虛無假設，時間序列為定態；圖16檢定統計量-6.075大於臨界值-2.880，拒絕虛無假設，時間序列為定態；圖17檢定統計量-6.272大於臨界值-3.430，拒絕虛無假設，時間序列為定態。</p><p><strong>4.4 選定落後期數</strong></p><p>由於本文欲採用VAR模型，故須先選定最適落後期數，結果如圖18所示。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/1*tvpEWW2u7RGo34dPws0ASQ.png" /><figcaption>圖18 選定最適落後期數</figcaption></figure><p>由圖18可知，在lag=4時，擁有最小的Information criteria(FPE,AIC,HQIC)，且同時達到統計上顯著，故採用VAR(4)模型。</p><p><strong>4.5 共整合檢定</strong></p><p>利用Johansen檢定認定共整合方程式數目，其結果如圖19所示。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/1*5xOAWpDKEdrLd3SaLwdn6g.png" /><figcaption>圖19 Johansen檢定</figcaption></figure><p>由圖19可知，在所有落後期，Trace統計量皆大於臨界值3.76，不拒絕無共整合之虛無假設，因此對數長期利率與對數通貨膨脹率不存在共整合方程式，故採用VAR模型進行分析。</p><p><strong>4.6 VAR(4)之數學式</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/639/1*kC-wB43IW-WRYKw22WS0tg.png" /><figcaption>圖20 VAR(4)模型－STATA結果</figcaption></figure><p>將圖20結果整理成數學式，如圖21所示。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/486/1*DtidAfrKZoz4HdEodAV3Iw.png" /><figcaption>圖21 VAR(4)模型數學式</figcaption></figure><p>將圖21畫成因果圖，如圖22所示。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/576/1*DpYrVFiTiFKIQtOGAxnx5Q.png" /><figcaption>圖22 VAR之因果圖</figcaption></figure><p>由圖22可知，變數之間不存在互相影響，唯有自身會影響當期數值。</p><p><strong>4.7 模型殘差檢定</strong></p><p><strong>4.7.1 常態性檢定</strong></p><p>使用Jarque-Bera常態性檢定，檢定VAR(4)模型殘差項是否達到常態，結果如圖23所示。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/473/1*vscVyAqnqJqfNN6Crp87vA.png" /><figcaption>圖23 VAR(4)模型殘差項之Jarque-Bera常態性檢定</figcaption></figure><p>由圖23可知，拒絕為常態之虛無假設，故為非常態。</p><p><strong>4.7.2 自我相關檢定</strong></p><p>使用Lagrange-multiplier檢定模型殘差項是否有自我相關，結果如圖24所示。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/326/1*nppXMllDNZicO5Yr1HxPOA.png" /><figcaption>圖24 VAR(4)模型殘差項之Lagrange-multiplier檢定</figcaption></figure><p>由圖24所示，除去落後期為2具有自我相關外，其他皆不具有自我相關。</p><p><strong>4.8 模型係數穩定性</strong></p><p>藉由繪製特徵值之圖型，判斷VAR(4)模型是否達到穩定性。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/1*nEBXwS6QLuBQuU-AJWcuSw.png" /><figcaption>圖25 模型特徵值之圖型</figcaption></figure><p>由圖25可看出，特徵值上下均勻分布，亦即VAR(4)模型估計而出的係數還算穩定。</p><p><strong>4.9 模型結構分析</strong></p><p><strong>4.9.1 Chow穩定性檢定</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/1*bZq4u41K6L0addojSe9njw.png" /><figcaption>圖26 Chow穩定性檢定</figcaption></figure><p>由圖26可知，轉折點在第141筆觀察值，但由於轉折點前後模型皆未達統計上顯著，故本文不將樣本拆成兩段進行分析。</p><p><strong>4.9.2 Granger因果檢定</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/548/1*PTIIUBQBsFJ_9NU-jM88hQ.png" /><figcaption>圖27 Granger因果檢定</figcaption></figure><p>由圖27可知，皆無法拒絕存在Granger因果關係之虛無假設，故對數長期利率與對數通貨膨脹率彼此皆不具有Granger因果關係。</p><p><strong>4.9.3 衝擊反應(irf)分析</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/1*eN91pHS3cdQZyNFzUjqNAQ.png" /><figcaption>圖28 對數長期利率衝擊對數通貨膨脹率之irf分析</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/1*rRlbqRlJnFV05GEK2YbdJw.png" /><figcaption>圖29 對數通貨膨脹率衝擊對數長期利率之irf分析</figcaption></figure><p>由圖28可知，對數長期利率會短期且來回衝擊對數通貨膨脹率，以前3期升幅最大，第4至第7期微幅震盪，第8期至第50期引響力大幅度增加，第50期後逐漸趨近於零，最後引響力近乎於零；由圖29可知，對數通貨膨脹率會短期且來回衝擊對數長期利率，以前3期降幅最大，第4至第7期微幅震盪，第8至第50期後引響力小幅度增加，第50期後逐漸趨近於零，最後引響力近乎於零。</p><p><strong>4.10 模型預測誤差變異之分解</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/664/1*SZMWx3ibFwjn4YNO9rGHBg.png" /><figcaption>圖30 預測誤差變藝術分解表－第一期與第二期</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/673/1*WKcp0haGW6TDl7VgbDp2jg.png" /><figcaption>圖31 預測誤差變藝術分解表－第三期與第四期</figcaption></figure><p>從圖30與圖31可知，對數長期利率受過去自身影響大，而隨著期數拉長，對數通貨膨脹率受過去自身影響將會有明顯幅度的下降。</p><p>附錄 : Stata Code</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/143c6af7a7547b0b07c104955bdebe20/href">https://medium.com/media/143c6af7a7547b0b07c104955bdebe20/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a80e2e9979f0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/yuying%E7%9A%84%E4%BD%9C%E5%93%81%E9%9B%86/stata-%E7%BE%8E%E5%9C%8B%E9%80%9A%E8%86%A8%E7%8E%87%E8%88%87%E9%95%B7%E6%9C%9F%E5%88%A9%E7%8E%87%E8%B6%A8%E5%8B%A2%E5%88%86%E6%9E%90-a80e2e9979f0">[STATA] 美國通膨率與長期利率趨勢分析</a> was originally published in <a href="https://medium.com/yuying%E7%9A%84%E4%BD%9C%E5%93%81%E9%9B%86">Works</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[STATA] 影響臺灣GDP的原因分析]]></title>
            <link>https://medium.com/yuying%E7%9A%84%E4%BD%9C%E5%93%81%E9%9B%86/stata-%E5%BD%B1%E9%9F%BF%E8%87%BA%E7%81%A3gdp%E7%9A%84%E5%8E%9F%E5%9B%A0%E5%88%86%E6%9E%90-68faa7c5e00b?source=rss-65a785c4c90a------2</link>
            <guid isPermaLink="false">https://medium.com/p/68faa7c5e00b</guid>
            <category><![CDATA[tata]]></category>
            <dc:creator><![CDATA[Airater]]></dc:creator>
            <pubDate>Fri, 26 Jun 2020 14:15:28 GMT</pubDate>
            <atom:updated>2020-07-29T12:10:37.721Z</atom:updated>
            <content:encoded><![CDATA[<h3>[Stata] 影響臺灣GDP的原因分析 with code</h3><p><em>本文使用Stata以Prais-Winsten迴歸進行分析。</em></p><blockquote><strong><em>1. 研究動機</em></strong></blockquote><p>GDP是衡量國家經濟活動的重要指標之一，本文選定幾個常見的項目與原因，試圖找出其中會影響臺灣GDP的變數。</p><blockquote><strong><em>2. 研究方法</em></strong></blockquote><p>私人消費 、投資 、勞動生產力、失業率和通貨膨脹率皆為可能影響GDP的原因，故本文建立以下迴歸式，如式(1)所示，其中各變數皆使用年增率(%)表示。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/802/1*KJipBLlyt4hShb_dLwFpuQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/329/1*TXXmyY03sUGju47Gw87X1A.png" /></figure><blockquote><strong><em>3. 資料來源</em></strong></blockquote><p>本文實證期間為西元1983年至2019年，資料頻率為年，皆取自於行政院主計總處。</p><blockquote><strong><em>4. 實證結果</em></strong></blockquote><p><strong>4.1 檢視資料</strong></p><p>首先，先檢視各變數的敘述統計，如圖1所示。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/1*k_ZzbHe1dElZ-zjoXwbYGQ.png" /><figcaption>圖1 敘述統計</figcaption></figure><p>接著列出變數間的相關係數，如圖2所示。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/1*IHSbs6l4CpMxdnWlakRe8w.png" /><figcaption>圖2 相關係數</figcaption></figure><p>由圖2可知，GDP年增率(gdp)與私人消費年增率(con)呈現高度正相關，但與失業率(unem)呈現高度負相關；私人消費年增率(con)與失業率(unem)呈現高度負相關。</p><p><strong>4.2 OLS 迴歸</strong></p><p>以OLS進行迴歸，其結果如圖3所示。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/1*SUnt7ujPAC4AIOnqMNusyA.png" /><figcaption>圖3 迴歸結果</figcaption></figure><p>在顯著水準0.05之下，私人消費年增率(con)、勞動生產力年增率(pro)及失業率(unem)達到統計上顯著，但該迴歸式的Adjusted R-squared不高，因此該模型解釋能力不高，故檢視變數是否為常態與線性(Linear)，若非為常態及線性則進行對數轉換。</p><p><strong>4.2.1 變數是否為常態</strong></p><p>以繪圖的方式判斷變數是否為常態，如圖4至圖9所示。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/1*urFylmLTLqModgM54jiEsA.png" /><figcaption>圖4 GDP年增率常態分佈圖</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/1*WTHbUavn66bFQ4MEPQKVUw.png" /><figcaption>圖5 私人消費年增率常態分佈圖</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/1*whLP2pqltaxKxaH6yhtgxA.png" /><figcaption>圖6 投資年增率常態分佈圖</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/1*jdMcGMxDEinYSp9jtZa4rA.png" /><figcaption>圖7 勞動生產力年增率常態分佈圖</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/1*cOd-TwIVmRlgbpW8t3uR0w.png" /><figcaption>圖8 失業率常態分佈圖</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/1*zz85iWifO83E2O9E2rcx8g.png" /><figcaption>圖9 通貨膨脹率常態分佈圖</figcaption></figure><p>由圖4至圖9可知，各變數皆未貼近常態分配曲線，為非常態。</p><p>4.2.2 <strong>自變數是否為線性</strong></p><p>運用STATA中的Arguemented component-plus-residual圖形，判斷自變數是否為線性，其結果如圖10至圖14所示。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/1*viXyUFYtGcNS1f5xPGLNdA.png" /><figcaption>圖10 私人消費年增率</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/1*C_fTsQRZj-Af2YHI6ilmnQ.png" /><figcaption>圖11 投資年增率</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/1*hWBMYYRudfATnbeotbHBBQ.png" /><figcaption>圖12 勞動生產力年增率</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/1*5YnLswTC8cD4c33YyQg9MQ.png" /><figcaption>圖13 失業率</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/1*1d4WAJxj1SYTIJ9TnHjb4w.png" /><figcaption>圖14 通貨膨脹率</figcaption></figure><p>由圖10至圖14可知，自變數皆偏離直線，非為線性。</p><p><strong>4.3 Log OLS 迴歸</strong></p><p>綜合4.2.1與4.2.2之內容，可得知迴歸式中的各個變數皆未達到常態與線性，因此取Natural log作為轉換，故將式(1)改寫為式(2)，如下所示。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/804/1*-03djXKb8BxlGs1cwxXz7Q.png" /></figure><p>對式(2)進行OLS迴歸，其結果如圖15所示。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/1*sJjhqNGCrCw5Xw-ruosuOQ.png" /><figcaption>圖15 對數迴歸結果</figcaption></figure><p>在顯著水準0.05之下，對數私人消費年增率(lncon)與對數勞動生產力年增率(lnpro)達到統計上顯著，亦即相較於其他自變數，此兩個變數會明顯影響GDP，而迴歸式Adjusted R-squared從式(1)的0.7128小幅度上升至0.7415。</p><p>欲知其他自變數是否對於應變數有顯著影響，可藉由檢定線性假說(Test Linear Hypothesis)判斷，其結果如圖16、圖17及圖18所示。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/357/1*yevIQB_mw-0tHBFT9eYFMA.png" /><figcaption>圖16 對數投資年增率檢定結果</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/313/1*FWNZLbWHK2I47Fj1bFNzbQ.png" /><figcaption>圖17 對數失業率檢定結果</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/332/1*UIrWd9b8fZp0c-PTlUuYfA.png" /><figcaption>圖18 對數通貨膨脹率檢定結果</figcaption></figure><p>由圖16、圖17及圖18可知，此三個自變數皆無法拒絕係數為0的虛無假設，故除了對數私人消費年增率(lncon)與對數勞動生產力年增率(lnpro)以外，迴歸式中的其他變數皆對於應變數無明顯影響。</p><p><strong>4.3.1 模型殘差項是否存在異質性現象</strong></p><p>OLS的其中一個假設為殘差同質性，若具異質性則改用Robust OLS進行迴歸，在此使用Breusch–Pagan test與White’s heteroscedasticity test檢定殘差項，其虛無假設皆為殘差不具異質性，結果分別如圖19與圖20所示。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/492/1*JMTqXN2IldyWd8olashEjg.png" /><figcaption>圖19 Breusch–Pagan test</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/433/1*HBhQLMhArYCHK0eLyt85gg.png" /><figcaption>圖20 White’s heteroscedasticity test</figcaption></figure><p>在顯著水準0.05之下，皆無法拒絕虛無假設，故此迴歸式殘差項不具有異質性。</p><p><strong>4.3.1 模型殘差項是否存在自我相關</strong></p><p>OLS的其中一個假設為殘差不存在自我相關，在此使用Breusch-Godfrey test檢定殘差項，結果如圖21所示。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/613/1*-Xu9WsUkuVDFwDjt3gjgsA.png" /><figcaption>圖21 Breusch-Godfrey test</figcaption></figure><p>在顯著水準0.05之下，拒絕不存在自我相關的虛無假設，故此迴歸式殘差項存在自我相關。</p><p><strong>4.4 Prais-Winsten迴歸</strong></p><p>由於迴歸式殘差項存在自我相關，故改以Prais-Winsten迴歸進行分析，其結果如圖22所示。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/637/1*A432i-x5gM3kumQuJDt4Fw.png" /><figcaption>圖22 Prais-Winsten迴歸結果</figcaption></figure><p>經查表後得知，在顯著水準為0.05之下，n=27且k=5時的上下界為[1.004,1.861]，迴歸式的Durbin-Watson <em>d</em>值約為1.37，落在界限範圍內，故殘差項無自我相關。</p><p>在顯著水準為0.05之下，對數私人消費年增率(lncon)及對數勞動生產力年增率(lnpro)皆達統計上顯著，且迴歸式Adjusted R-squared從式(2)的0.7415上升至0.8181。</p><p><strong>4.5 結論</strong></p><p>本文選定私人消費 、投資 、勞動生產力、失業率和通貨膨脹率，為可能影響臺灣GDP原因，經Prais-Winsten迴歸分析後，得知私人消費與勞動生產力為明顯影響GDP之變數，其結果相當符合經濟常理，因私人消費在GDP中佔大約一半的比例，如圖23所示。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/841/1*L3J4e0GgIB_loIGSTqIcTA.png" /><figcaption>圖23 西元1983年至2019年臺灣私人消費與GDP</figcaption></figure><p>勞動生產力定義為一小時勞工所生產之實質經濟產出(real economic output per labor hour)，故此變數無庸置疑地會是影響GDP之因素。</p><blockquote><strong><em>附錄 : Stata Code</em></strong></blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8fc87bc794216efa8ecbe77ca8619421/href">https://medium.com/media/8fc87bc794216efa8ecbe77ca8619421/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=68faa7c5e00b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/yuying%E7%9A%84%E4%BD%9C%E5%93%81%E9%9B%86/stata-%E5%BD%B1%E9%9F%BF%E8%87%BA%E7%81%A3gdp%E7%9A%84%E5%8E%9F%E5%9B%A0%E5%88%86%E6%9E%90-68faa7c5e00b">[STATA] 影響臺灣GDP的原因分析</a> was originally published in <a href="https://medium.com/yuying%E7%9A%84%E4%BD%9C%E5%93%81%E9%9B%86">Works</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>