การย้าย ค่าเฉลี่ย การคาดการณ์ ใน r


R - Forecasting Approaches to Forecasting ARITA (AutoRegresive Integrated Moving Average) ETS (แบบจำลองช่องว่างแบบ Exponential) เราจะพูดถึงวิธีการใช้งานและวิธีการเหล่านี้ ภาพรวมพร็อพเพอร์ตี้พยากรณ์แก้ไขคำอธิบายการแก้ไข Smonting คำอธิบายชื่อ AKA: ค่าเฉลี่ยเคลื่อนที่แบบถ่วงน้ำหนักที่ถ่วงน้ำหนัก (EWMA) เทียบเท่ากับรูปแบบ ARIMA (0,1,1) โดยไม่มีค่าคงที่ระยะเวลาที่ใช้สำหรับข้อมูลที่ราบรื่นสำหรับการนำเสนอทำให้การคาดการณ์ค่าเฉลี่ยเคลื่อนที่ง่าย: การสังเกตในอดีตมีการถ่วงน้ำหนักเท่ากัน smoothing: กำหนดค่าน้ำหนักที่ลดลงเรื่อย ๆ เมื่อเวลาผ่านไปสูตร xt - ข้อมูลดิบลำดับ st - เอาต์พุตของอัลกอริธึมการแจกแจงแบบเอกซ์โพเนนเชียล (ประมาณค่าถัดไปของ x) - ปัจจัยการปรับให้เรียบ 0160lt160160lt1601 การเลือกใช้วิธีทางสถิติอย่างถูกต้องอาจไม่ใช้วิธีทางสถิติเพื่อเพิ่มมูลค่าให้มากที่สุด (เช่น OLS) ยิ่งใหญ่เท่าไหร่ก็ยิ่งใกล้จะได้รับการคาดการณ์แบบไร้เดียงสา (พอร์ตเดียวกับชุดเดิมที่มีระยะเวลาล้าหลัง) Double Exponential Smoothing แก้ไขง่าย (มีความลำเอียงอยู่เสมอ) การเรียบเป็นคู่เป็นกลุ่มของวิธีการจัดการกับปัญหา Holt-Winters การแก้ไขการแกว่งเป็นสองเท่าและสำหรับ t gt 1 โดยที่เป็นปัจจัยการทำให้เรียบของข้อมูล 0160lt160160lt1601 และเป็นตัวปรับความเรียบของแนวโน้ม 0160lt160160lt1601 Output F tm - การประมาณค่าของ x ในเวลา tm, mgt0 ขึ้นอยู่กับข้อมูลดิบถึงเวลา t การปรับการชดเชยความคมชัดแบบ Triple Exponential เรียบเรียงโดยคำนึงถึงการเปลี่ยนแปลงตามฤดูกาลรวมทั้งแนวโน้มที่แนะนำโดย Holts student, Peter Winters, in 1960 Input xt - ข้อมูลดิบลำดับของการสังเกต t 1601600 L ยาววงจรของการเปลี่ยนแปลงตามฤดูกาลวิธีการคำนวณ: เส้นแนวโน้มสำหรับข้อมูลตามฤดูกาลดัชนีที่น้ำหนักค่าในเส้นแนวโน้มตามที่จุดเวลานั้นตกอยู่ในวงจรของความยาว L s t หมายถึงค่าที่ราบรื่นของส่วนที่คงที่สำหรับเวลา t bt หมายถึงลำดับของการประมาณค่าที่ดีที่สุดของแนวโน้มเชิงเส้นที่ซ้อนทับกับการเปลี่ยนแปลงตามฤดูกาล ct คือลำดับของปัจจัยการแก้ไขตามฤดูกาล ct เป็นสัดส่วนที่คาดการณ์ไว้ของแนวโน้มที่คาดการณ์ได้ตลอดเวลา t mod L ในรอบที่การสังเกตดำเนินการเมื่อถึง เริ่มต้นดัชนีตามฤดูกาล c tL ต้องมีอย่างน้อยหนึ่งรอบที่สมบูรณ์ในข้อมูลผลลัพธ์ของอัลกอริทึมจะถูกเขียนขึ้นใหม่เป็น F tm การประมาณค่าของ x ที่เวลา tm, mgt0 ขึ้นอยู่กับข้อมูลดิบถึงเวลา t การคำนวณความเร่งด่วนแบบ Triple Exponential จะได้จากสูตรที่เป็นตัวคูณข้อมูล 0160lt160160lt1601 เป็นตัวปรับความเรียบของแนวโน้ม 0160lt160160lt1601 และเป็นปัจจัยการปรับฤดูกาลตามฤดูกาล 0160lt160160lt1601 สูตรทั่วไปสำหรับการประมาณการแนวโน้มเริ่มต้น b 0 คือ: การกำหนดค่าเริ่มต้นสำหรับดัชนีตามฤดูกาล c i สำหรับ i 1,2 L เกี่ยวข้องมากขึ้น ถ้า N คือจำนวนรอบที่สมบูรณ์ในข้อมูลของคุณ: โปรดทราบว่า A j คือค่าเฉลี่ยของ x ในรอบ j ของข้อมูลของคุณ ETS แก้ไขพารามิเตอร์แทนที่แทนที่ 8.4 การย้ายโมเดลเฉลี่ยแทนที่จะใช้ค่าที่ผ่านมาของตัวแปรคาดการณ์ในการถดถอยโมเดลค่าเฉลี่ยเคลื่อนที่จะใช้ข้อผิดพลาดในการคาดการณ์ที่ผ่านมาในรูปแบบการถดถอยเหมือนกัน y c et theta e theta e จุด theta e ที่ et มีเสียงสีขาว เราอ้างถึงนี้เป็นรูปแบบ MA (q) แน่นอนว่าเราไม่ได้สังเกตค่าของเอตดังนั้นจึงไม่ใช่การถดถอยตามความหมายปกติ สังเกตว่าแต่ละค่าของ yt สามารถคิดได้ว่าเป็นค่าเฉลี่ยถ่วงน้ำหนักของข้อผิดพลาดในการคาดการณ์ที่ผ่านมา อย่างไรก็ตามแบบจำลองค่าเฉลี่ยเคลื่อนที่ไม่ควรสับสนกับการปรับค่าเฉลี่ยการเคลื่อนที่โดยเฉลี่ยที่เรากล่าวถึงในบทที่ 6 โมเดลเฉลี่ยถ่วงน้ำหนักใช้สำหรับคาดการณ์ค่าในอนาคตขณะที่ใช้การปรับค่าเฉลี่ยโดยเฉลี่ยเพื่อใช้ประเมินแนวโน้มรอบของค่าในอดีต รูปที่ 8.6: ตัวอย่างสองตัวอย่างของข้อมูลจากโมเดลเฉลี่ยเคลื่อนที่ที่มีพารามิเตอร์ต่างกัน ซ้าย: MA (1) กับ y t 20e t 0.8e t-1 ขวา: MA (2) ด้วย y t e t - e t -1 0.8e t-2 ในทั้งสองกรณี e t จะกระจายสัญญาณรบกวนสีขาวเป็นปกติโดยมีค่าเฉลี่ยศูนย์และค่าความแปรปรวน 1 รูปที่ 8.6 แสดงข้อมูลบางส่วนจากแบบจำลอง MA (1) และ MA (2) การเปลี่ยนพารามิเตอร์ theta1, dots, thetaq ส่งผลให้รูปแบบชุดเวลาต่างกัน เช่นเดียวกับโมเดลอัตถดถอยความแปรปรวนของเทอมข้อผิดพลาด et จะเปลี่ยนขนาดของชุดไม่ใช่รูปแบบ สามารถเขียนแบบ AR (p) แบบ stationary เป็นแบบ MA (infty) ได้ ตัวอย่างเช่นการใช้การทดแทนซ้ำเราสามารถแสดงให้เห็นถึงรูปแบบ AR (1) นี้: เริ่มต้นแอ็พพลิเคชัน amp phi1y et amp phi1 (phi1y e) และ amp phi12y phi1 e และ amp phi13y phi12e phi1 e และ amptext end Provided -1 lt phi1 lt 1 ค่าของ phi1k จะเล็กลงเมื่อ k มีขนาดใหญ่ขึ้น ดังนั้นในที่สุดเราจึงได้รับ yt et phi1 e phi12 e phi13 e cdots กระบวนการ MA (infty) ผลย้อนกลับถือถ้าเรากำหนดข้อ จำกัด บางประการเกี่ยวกับพารามิเตอร์ MA จากนั้นแบบจำลอง MA เรียกว่า invertible นั่นคือเราสามารถเขียนกระบวนการ MA (q) invertible เป็นกระบวนการ AR (infty) ได้ โมเดลที่ไม่สามารถผันกลับไม่ได้ทำให้เราสามารถแปลงจากโมเดล MA ไปเป็น AR ได้ พวกเขายังมีคุณสมบัติทางคณิตศาสตร์บางอย่างที่ช่วยให้สามารถใช้งานได้ง่ายขึ้น ข้อ จำกัด invertible มีความคล้ายคลึงกับข้อ จำกัด stationarity สำหรับแบบจำลอง MA (1): -1lttheta1lt1 สำหรับโมเดล MA (2): -1lttheta2lt1, theta2theta1 gt-1, theta1 - theta2 lt 1. เงื่อนไขที่ซับซ้อนมากขึ้นถือได้สำหรับ qge3 อีกครั้ง R จะดูแลข้อ จำกัด เหล่านี้เมื่อประมาณแบบจำลองการคาดการณ์โดยเฉลี่ยแบบจำลองการย้ายแบบจำลองการคาดการณ์โดยเฉลี่ยเป็นเครื่องมือที่มีประสิทธิภาพซึ่งจะช่วยให้ผู้จัดการในการตัดสินใจเกี่ยวกับการคาดการณ์ที่ได้รับการศึกษา ค่าเฉลี่ยเคลื่อนที่จะถูกใช้เป็นหลักในการคาดการณ์ข้อมูลช่วงสั้นในอดีต เครื่องมือนี้พร้อมกับเครื่องมือคาดการณ์อื่น ๆ เช่นคอมพิวเตอร์ใน Excel ซึ่งทำให้ใช้งานง่าย สำหรับการพยากรณ์อากาศเฉลี่ยเคลื่อนที่ให้อ่านงานต่อไปนี้ ขอรับข้อมูลราคารายวันในช่วงห้าปีที่ผ่านมาสำหรับหุ้นที่แตกต่างกันสามแห่ง ข้อมูลสามารถรับได้จากอินเทอร์เน็ตโดยใช้คำหลักต่อไปนี้: ข้อมูลราคาหุ้นข้อมูลย้อนกลับข้อมูลของ บริษัท และผลตอบแทนของหุ้น สร้างค่าเฉลี่ยของแนวโน้มการเคลื่อนที่โดยใช้ค่าต่อไปนี้: 10, 100 และ 200 กราฟข้อมูลด้วย Excel สร้างค่าเฉลี่ยที่ศูนย์กลางการย้ายโดยใช้ค่าต่อไปนี้: 10, 100 และ 200 กราฟข้อมูลด้วย Excel ค่าเฉลี่ยเคลื่อนที่ของค่าเฉลี่ยของ m เปรียบเทียบระหว่างค่าเฉลี่ยของแนวโน้มการเคลื่อนที่และค่าเฉลี่ยที่เคลื่อนไหวตรงกลางอธิบายว่าค่าเฉลี่ยเคลื่อนที่เหล่านี้สามารถช่วยนักวิเคราะห์หุ้นในการกำหนดทิศทางราคาหุ้นได้อย่างไร ให้คำอธิบายโดยละเอียดพร้อมด้วยเหตุผล ส่งคำตอบของคุณในเอกสาร Word แปดถึงสิบหน้าและในแผ่นงาน Excel ในหน้าอื่น ๆ ให้อ้างอิงแหล่งข้อมูลทั้งหมดโดยใช้หลักเกณฑ์ APA โซลูชันรายละเอียด (Text amp EXCEL) ตัวอย่างการรับประกันคุณภาพ (0 คำ) ดูตัวอย่าง file1.docx (564 คำ) xxxxx xxxxxxx ต่างกันคือ xxxxxx xxxxxx และ Microsoft เลือกสำหรับการวิเคราะห์ความคาดการณ์ xxxx และ xxxxxxx ทุกวัน xxxxxx xxxxxx xxxxxx xxxxxxxxxx ของ xxxx ห้า xxxxx (1 xxxxxxxxrsquo2008 เพื่อ xxxx xxxxx พฤศจิกายนได้รับ xxx ค่าเฉลี่ยเคลื่อนที่ XXXXXXXX xxxxxxxxxxx xxxxxx แนวโน้มค่าเฉลี่ย xxx xxxxxxxx การเป็นศูนย์กลางการเคลื่อนไหวที่คาดการณ์ xxxxxxx ถูก xxxx xx วิเคราะห์คาดการณ์ xxxxx XXXXXXXXXXXX xxxxxxx. xxx XXXXXXXXXXXX xxxxxxxxx เฉลี่ย xxxxxxxxx ใน xxxxxx xxxxxxx xxxxx xx สิ่งที่แนบมา EXCEL xxx xxx xxxx xxx คาดการณ์กราฟต่อไปนี้แสดง x 10 รอบระยะเวลา 100 และ x xxxxxxxxxx xxxxxxxxxxxx เฉลี่ย xxxxxxxx xxx ปิด xxxxx xxx xxxxx xxxxxx xxxxxxxxx xxxxx แสดง x 10- รอบระยะเวลา xxx 100 รอบ x xxxxxxxxxx xxxxxxxxxxxx การคาดการณ์โดยเฉลี่ยสำหรับราคาปิด xx xxxxxx xxx ของ Google แสดงกราฟต่อไปนี้ x 10 รอบ xxxxxxxxxx xxx xxxxxxxxxx xxxxxxxxx xxx xxxxxxx คาด xxx ราคาปิด xx xxxxxxxxx หุ้น: xxxxxxxxxxxxxx xxxxxxx คาดการณ์: - - - ข้อความเพิ่มเติมดังนี้ - - - ตัวอย่าง file2.xlsx (17127 คำ) - - - ยาวเกินไปเพื่อแสดงตัวอย่าง - - - ตัวอย่างเนื้อหา (0 words) file1.docx ภาพตัวอย่าง (966 คำ) ย้าย xxxxxxx xxxxxxxxxxx รุ่น xxxx xxxx xxxxxxxxx ของการศึกษาคือ xxxx xx xxx xxx แนวคิด xx xxxxx ค่าเฉลี่ยเคลื่อนที่และศูนย์กลาง xxxxxx เฉลี่ย xxxxxxxxxxx xxxxxxx xxx xxxxxxx xx เหมาะสม xxxxxx xxx XXXXXXXXXXXX xxx xxx xxxxxxx นี้ xxxxxx xx เลือก xxxxx ไอที xxxxxxxxxx คืออินโฟซิส xxxxx xxx xxxxxxx xxxxxxxxxx ราคา xxxx yahoo XXXXXXXX ข้อมูล xxx xxxx xxxxxxxxx งวด xx 11 มกราคม 2010 xx xxx xxx 2014 xxxxx xxxxxx XXXXXXXX เทรนด์ย้าย xxxxxxx xx หนึ่ง xxx xxxx ใช้กันอย่างแพร่หลาย xxxxxxxxx xx xxxxxxxxx XXXXXXXX xxx มัน xxxxx xx xxxxxx เสียงจาก xxxxxxx xxxxxxxxxxxxx แบบสุ่ม xxxxxxxxxxx นี้ใช้ xxxxxxx ในอดีต xxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx ค่าเฉลี่ยเคลื่อนที่เฉลี่ย ในค่าเฉลี่ย xxxxxx แบบง่ายๆ xxx xxxxx xxx xxxxxxxxxx xxx xxxxxx เฉลี่ย xx xxx ndash 1) ค่า xxx xxx xxx ต่อเนื่อง - - - ยาวเกินกว่าที่จะแสดงตัวอย่าง - - - การพยากรณ์อากาศเฉลี่ย 100 ครั้งคำตอบที่ถูกต้องคำตอบจาก Word-doc และ excel การแสดงตัวอย่างเนื้อหา (9 คำ) xxxxxx xxxxxxx xxxxxxxxxxx xxxx xxxxxxx ตอบคำ doc แสดงตัวอย่าง xxx xxxxx xx file1.doc (231 คำ) ได้รับข้อมูลราคา xxxxx xxxx ผ่านมาห้าปีสามข้อมูลที่แตกต่างกัน xxxxxxx xxx xx XXXXXXXX xxxx XXXXXXXX โดยใช้ xxx ต่อไป xxxxxxxxx ข้อมูลราคา xxxxx ข้อมูล xxxxxx ที่ ข้อมูล บริษัท และผลตอบแทนของหุ้น xxx xxxx xx ได้ xxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxx ข้อมูลทางประวัติศาสตร์ xxxx xxx บริษัท xxxxx xxx xxxxxxx xxx xxx บริษัท ที่ผ่านมาราคา xxx xxxxx จะได้รับสำหรับ xxxxxxx UTI Worldwide xxxx (UTIW) xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxampxxxxampxxxampc2006ampxxxxampxxxampxxxxxxampgd xxxxxx แนวโน้มเคลื่อนไหว XXXXXXXX xxxx xxx xxxxxxxxx ค่า xxx xx 10 xxxx และ 200 ข้อมูล xxxxx xxx xxxx xxxxxxxxx xxxxxx สร้างศูนย์กลางการเคลื่อนไหว XXXXXXXX xxxx xxxxxx ต่อไปนี้สำหรับ xx 10, 100, xxx 200 กราฟ xxx xxxx กับ xxxxxxxxx Excel xxx xxx xxx xxx xxx xxx xxx xxxxx x x xxx xxx xxx xxx xxxxxxxxx xxxxxxx xxxxxxxxx xxxxxxx xxxxxxxxxx xxxxxxx xxxxxxx xxx xxxxx xxxxxx xxxxx xxxxxx xxxxx xxx xxx xxxxx xxx xxxxx xxx xxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx xxxxxxx. xls ตัวอย่าง (150 คำ) - - - ยาวเกินไปที่จะแสดงตัวอย่าง - - - ตัวแสดงตัวอย่าง (6 คำ) xxxxxx ดูไฟล์ที่แนบ xxx xxxxxxx ดูตัวอย่าง file1.xlsx (89 คำ) - - - ยาวเกินไปเพื่อแสดงตัวอย่าง - - - ตัวอย่างไฟล์. docs (คำ 761 คำ) xxxxxx เฉลี่ย xxxxxxxxxxx โมเดล ModelsMoving เฉลี่ย xxxxxxxxxxx เป็นโมเดล xxxxx ที่มีประสิทธิภาพซึ่งผู้จัดการ xxxx ในการตัดสินใจ xxxxxx xxxxxxxx xxxxxxxxxxx xxxxxx เฉลี่ย xx ใช้เป็นหลัก xxxxxxx xxxxx ช่วงสั้น ๆ ในเชิงประวัติ xxxx นี้พร้อมกับ xxxxx เครื่องมือ xxxxxxxxxxx เป็น xxx xxxx คอมพิวเตอร์ใน xxxxxx ซึ่งทำให้ xx ใช้งานง่าย เรื่อง xx ย้ายการคาดการณ์เฉลี่ยกับ xxxx xxxxxxxxx xxxxxxxxxxxxxxxxx ข้อมูลซกซ์ซกซ์กว่า xxx xxxx ห้า xxxxx xxx สาม xxxxxxxxx xxxxxxx ข้อมูล xxx xx XXXXXXXX xxxx XXXXXXXX xx ใช้คำหลัก xxx xxxxxxxxx: ข้อมูลหุ้น xxxxx บริษัท กลับมา xxxxx xxxxx xxx xxxxx xxxxxxxxxxxbullxxxxxx XXXXXXXXXXXX XXXXXXXX xxxx รูปแบบค่าต่อไปนี้: 10, xxxx xxx 200 กราฟข้อมูล xxxx xxxx Excel. bullxxxxxx xxxxxxxxxxxxxxx ค่าเฉลี่ย xxxx xxx ค่าต่อไปนี้รูปแบบ: xxx 100, xxx 200 กราฟ xxx xxxx xxxx xxxxxxxxxbullxxx ทำ xxx เคลื่อนที่เฉลี่ย xxx xxx xxxxxx เดียวกันของ m เปรียบเทียบระหว่าง x ใช้ xxxxxxxxxxxxx xxxxxxxxxxxbullxxxxxxx xxxxxxxxxxxxxxx xxxxxxxxxxxbullxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx การวิเคราะห์ข้อมูลชุดเวลา หนังสือเล่มนี้สันนิษฐานว่าผู้อ่านมีความรู้พื้นฐานเกี่ยวกับการวิเคราะห์อนุกรมเวลาและจุดสนใจหลักของหนังสือเล่มนี้ไม่ใช่เพื่ออธิบายถึงการวิเคราะห์อนุกรมเวลา แต่อธิบายถึงวิธีการวิเคราะห์เหล่านี้โดยใช้ R. ถ้าคุณยังใหม่กับซีรีส์เวลา การวิเคราะห์และต้องการเรียนรู้เพิ่มเติมเกี่ยวกับแนวคิดใด ๆ ที่นำเสนอในที่นี้ฉันขอแนะนำหนังสือ 8220Time series8221 ของ Open University (รหัสผลิตภัณฑ์ M24902) ที่มีจำหน่ายจาก Open University Shop ในหนังสือเล่มนี้ฉันจะใช้ชุดข้อมูลชุดข้อมูลตามเวลาที่ Rob Hyndman ได้รับในไลบรารีข้อมูล Time Series ของเขาที่ robjhyndmanTSDL หากคุณชอบหนังสือเล่มนี้คุณอาจต้องการดูหนังสือเล่มเล็ก ๆ เกี่ยวกับการใช้ R สำหรับสถิติทางการแพทย์ชีวประวัติของฉัน - a-little-book-of-for-biomedical-statistics. readthedocs. org และหนังสือเล่มเล็ก ๆ ของฉันเกี่ยวกับการใช้ R สำหรับการวิเคราะห์แบบหลายตัวแปร, little-book-of-for-for-ultivariate-analysis. readthedocs. org การอ่านข้อมูลอนุกรมเวลาสิ่งแรกที่คุณจะต้องทำเพื่อวิเคราะห์ข้อมูลชุดเวลาของคุณคือการอ่านข้อมูลลงใน R และวางแผนชุดข้อมูลเวลา คุณสามารถอ่านข้อมูลใน R โดยใช้ฟังก์ชั่น scan () ซึ่งจะถือว่าข้อมูลของคุณอยู่ในแฟ้มข้อความแบบง่ายๆด้วยคอลัมน์เดียว ตัวอย่างเช่นไฟล์ robjhyndmantsdldatamisckings. dat มีข้อมูลเกี่ยวกับอายุการเสียชีวิตของกษัตริย์ต่อเนื่องของอังกฤษโดยเริ่มจาก William Conqueror (ต้นฉบับ: Hipel และ Mcleod, 1994) ชุดข้อมูลมีลักษณะดังนี้: เฉพาะบรรทัดแรกของไฟล์เท่านั้นที่แสดงขึ้น สามบรรทัดแรกมีความคิดเห็นเกี่ยวกับข้อมูลบางส่วนและเราต้องการละเว้นข้อมูลนี้เมื่อเราอ่านข้อมูลลงใน R. เราสามารถใช้พารามิเตอร์ 8220skip8221 ของฟังก์ชัน scan () ซึ่งระบุจำนวนบรรทัดที่ด้านบนของ ไฟล์ที่จะละเว้น หากต้องการอ่านไฟล์นี้ลงใน R โดยไม่สนใจบรรทัดแรกสามบรรทัดเราจะพิมพ์: ในกรณีนี้อายุของราชาภิเษก 42 แห่งของอังกฤษถูกอ่านลงในตัวแปร 8216kings8217 เมื่อคุณอ่านข้อมูลชุดข้อมูลเวลาเป็น R แล้วขั้นตอนต่อไปคือการเก็บข้อมูลในชุดข้อมูลอนุกรมเวลาใน R เพื่อให้คุณสามารถใช้ R8217s หลายฟังก์ชันในการวิเคราะห์ข้อมูลชุดข้อมูลเวลา ในการจัดเก็บข้อมูลในวัตถุแบบอนุกรมเวลาเราใช้ฟังก์ชัน ts () ใน R. ตัวอย่างเช่นเพื่อเก็บข้อมูลในตัวแปร 8216kings8217 เป็นชุดข้อมูลอนุกรมเวลาใน R เราพิมพ์: บางครั้งชุดข้อมูลชุดเวลาที่คุณ อาจได้รับการเก็บรวบรวมเป็นระยะ ๆ ซึ่งน้อยกว่าหนึ่งปีเช่นรายเดือนหรือรายไตรมาส ในกรณีนี้คุณสามารถระบุจำนวนครั้งที่มีการรวบรวมข้อมูลต่อปีโดยใช้พารามิเตอร์ 8216frequency8217 ในฟังก์ชัน ts () สำหรับข้อมูลชุดข้อมูลรายเดือนคุณตั้งความถี่ 12 ในขณะที่ข้อมูลชุดข้อมูลรายไตรมาสจะกำหนดความถี่ 4 นอกจากนี้คุณสามารถระบุปีแรกที่มีการรวบรวมข้อมูลและช่วงแรกในปีนั้นโดยใช้พารามิเตอร์ 8216start8217 ในฟังก์ชัน ts () ตัวอย่างเช่นถ้าจุดข้อมูลแรกตรงกับไตรมาสที่สองของปี 1986 คุณจะตั้งค่าเริ่มต้น (1986,2) ตัวอย่างคือชุดข้อมูลจำนวนการเกิดต่อเดือนในนิวยอร์กซิตี้ตั้งแต่เดือนมกราคม พ. ศ. 2489 ถึงธันวาคม พ. ศ. 2502 (ซึ่งเดิมเก็บรวบรวมโดยนิวตัน) ข้อมูลนี้มีอยู่ในไฟล์ robjhyndmantsdldatadatanybirths. dat เราสามารถอ่านข้อมูลใน R และจัดเก็บข้อมูลเป็นชุดข้อมูลอนุกรมเวลาได้โดยการพิมพ์: ในทำนองเดียวกันไฟล์ robjhyndmantsdldatadatafancy. dat มียอดขายรายเดือนสำหรับร้านขายของที่ระลึกในเมืองริมหาดใน Queensland, Australia สำหรับมกราคม 1987 - ธันวาคม 1993 (ข้อมูลต้นฉบับจาก Wheelwright และ Hyndman, 1998) เราสามารถอ่านข้อมูลใน R ได้โดยการพิมพ์: Plotting Time Series เมื่อคุณอ่านชุดข้อมูลเวลาเป็น R แล้วขั้นตอนต่อไปคือการทำพล็อตข้อมูลชุดข้อมูลเวลาซึ่งคุณสามารถทำอะไรได้ด้วย plot. ts () ใน R. ตัวอย่างเช่นเพื่อพล็อตชุดเวลาของอายุของการตายของ 42 พระมหากษัตริย์ต่อเนื่องของอังกฤษเราพิมพ์: เราสามารถดูได้จากพล็อตครั้งที่ชุดเวลานี้อาจจะมีการอธิบายโดยใช้แบบจำลอง additive ตั้งแต่ความผันผวนแบบสุ่ม ในข้อมูลมีค่าคงที่โดยประมาณเมื่อเทียบกับช่วงเวลา ในทำนองเดียวกันในการวางแผนจำนวนเวลาที่เกิดจำนวนต่อเดือนในเมืองนิวยอร์กเราจะพิมพ์: เราเห็นได้จากชุดข้อมูลในช่วงเวลานี้ว่าอาจมีการเปลี่ยนแปลงตามฤดูกาลในจำนวนการเกิดต่อเดือน: มีจุดสูงสุดในทุกฤดูร้อน และรางน้ำทุกฤดูหนาว อีกครั้งดูเหมือนว่าชุดข้อมูลในครั้งนี้น่าจะได้รับการอธิบายโดยใช้โมเดล additive เนื่องจากความผันผวนตามฤดูกาลมีค่าคงที่โดยประมาณเมื่อเทียบกับช่วงเวลาและดูเหมือนจะไม่ขึ้นอยู่กับระดับของชุดข้อมูลเวลาและความผันผวนแบบสุ่มก็ดูเหมือนจะเป็น มีขนาดคงที่ตลอดเวลา ในทำนองเดียวกันในการจัดทำยอดขายรายเดือนสำหรับร้านขายของที่ระลึกในเมืองชายหาดในรัฐควีนส์แลนด์ประเทศออสเตรเลียเราพิมพ์: ในกรณีนี้ปรากฏว่าโมเดล additive ไม่เหมาะสำหรับการอธิบายชุดเวลานี้เนื่องจากขนาด ของความผันผวนตามฤดูกาลและความผันผวนแบบสุ่มดูเหมือนจะเพิ่มขึ้นตามระดับของชุดข้อมูล ดังนั้นเราอาจจำเป็นต้องเปลี่ยนชุดเวลาเพื่อให้ได้ชุดเวลาที่เปลี่ยนแปลงซึ่งสามารถอธิบายได้โดยใช้แบบจำลอง additive ตัวอย่างเช่นเราสามารถแปลงชุดข้อมูลเวลาโดยการคำนวณ log ธรรมชาติของข้อมูลเดิม: ที่นี่เราจะเห็นว่าขนาดของความผันผวนตามฤดูกาลและความผันผวนแบบสุ่มในชุดข้อมูลที่เปลี่ยนเป็นข้อมูลบันทึกมีแนวโน้มที่จะคงที่ตลอดเวลาและทำ ไม่ขึ้นอยู่กับระดับของชุดข้อมูลเวลา ดังนั้นซีรีส์เวลาที่เปลี่ยนเป็นข้อมูลเข้าสู่ระบบสามารถอธิบายได้โดยใช้แบบจำลอง additive การสลายตัวของซีรีส์เวลาการสลายชุดข้อมูลเวลาจะหมายถึงการแยกองค์ประกอบออกเป็นส่วนประกอบซึ่งมักเป็นส่วนประกอบของแนวโน้มและส่วนประกอบที่ผิดปกติและถ้าเป็นชุดฤดูกาลตามฤดูกาลองค์ประกอบตามฤดูกาล การย่อยสลายข้อมูลที่ไม่ใช่ฤดูกาลข้อมูลชุดเวลานอกเวลาตามฤดูกาลประกอบด้วยองค์ประกอบแนวโน้มและองค์ประกอบที่ไม่ต่อเนื่อง การสลายชุดข้อมูลเวลาเกี่ยวข้องกับการพยายามแยกชุดข้อมูลเวลาออกเป็นองค์ประกอบเหล่านี้นั่นคือการประมาณส่วนประกอบเทรนด์และองค์ประกอบที่ไม่สม่ำเสมอ ในการประมาณองค์ประกอบเทรนด์ของซีรีส์เวลาที่ไม่ใช่ฤดูกาลซึ่งสามารถอธิบายได้โดยใช้โมเดล additive เป็นเรื่องปกติที่จะใช้วิธีการปรับให้ราบเรียบเช่นการคำนวณค่าเฉลี่ยเคลื่อนที่แบบเรียบง่ายของชุดข้อมูลเวลา ฟังก์ชัน SMA () ในแพคเกจ 8220TTR8221 R สามารถใช้เพื่อให้ข้อมูลชุดข้อมูลเป็นไปอย่างราบรื่นโดยใช้ค่าเฉลี่ยเคลื่อนที่แบบเรียบ ในการใช้ฟังก์ชันนี้เราต้องติดตั้งแพ็คเกจ 8220TTR8221 R (สำหรับคำแนะนำในการติดตั้งแพคเกจ R โปรดดูที่การติดตั้งแพคเกจ R) เมื่อติดตั้งแพ็คเกจ 8220TTR8221 R แล้วคุณสามารถโหลดแพคเกจ 8220TTR8221 R ได้โดยการพิมพ์: จากนั้นคุณสามารถใช้ฟังก์ชัน 8220SMA () 8221 เพื่อให้ข้อมูลชุดข้อมูลเป็นไปอย่างราบรื่น ในการใช้ฟังก์ชัน SMA () คุณต้องระบุลำดับ (ช่วง) ของค่าเฉลี่ยเคลื่อนที่โดยใช้พารามิเตอร์ 8220n8221 ตัวอย่างเช่นในการคำนวณค่าเฉลี่ยเคลื่อนที่แบบง่ายของลำดับที่ 5 เรากำหนด n5 ในฟังก์ชัน SMA () ยกตัวอย่างเช่นตามที่ได้กล่าวไว้ข้างต้นชุดเวลาของอายุของราชาภิเษกที่ 42 ของอังกฤษปรากฏไม่ใช่ฤดูกาลและสามารถอธิบายได้โดยใช้แบบจำลอง additive เนื่องจากความผันผวนของข้อมูลที่สุ่มมีขนาดคงที่โดยประมาณ เวลา: ดังนั้นเราสามารถลองคำนวณคอมโพเนนต์แนวโน้มของซีรีส์เวลานี้ได้โดยการให้เรียบโดยใช้ค่าเฉลี่ยเคลื่อนที่ที่เรียบง่าย เพื่อเรียบชุดเวลาโดยใช้ค่าเฉลี่ยเคลื่อนที่ที่เรียบง่ายของคำสั่งที่ 3 และพล็อตข้อมูลชุดข้อมูลที่ราบรื่นเราพิมพ์: ยังคงดูเหมือนจะเป็นจำนวนมากของความผันผวนแบบสุ่มในชุดเวลาที่เรียบโดยใช้ค่าเฉลี่ยเคลื่อนที่ที่เรียบง่ายของคำสั่งที่ 3 ดังนั้นเพื่อประมาณการคอมโพเนนต์แนวโน้มอย่างแม่นยำมากขึ้นเราอาจต้องการลองทำให้ข้อมูลเรียบโดยมีค่าเฉลี่ยเคลื่อนที่ที่เรียบง่ายของคำสั่งซื้อที่สูงขึ้น ใช้เวลาในการทดลองและข้อผิดพลาดเล็กน้อยเพื่อค้นหาจำนวนที่เหมาะสมของการทำให้เรียบ ตัวอย่างเช่นเราสามารถลองใช้ค่าเฉลี่ยเคลื่อนที่แบบง่ายของคำสั่งที่ 8: ข้อมูลที่เรียบโดยมีค่าเฉลี่ยเคลื่อนที่ที่เรียบง่ายของคำสั่งที่ 8 จะให้ภาพที่ชัดเจนขึ้นขององค์ประกอบแนวโน้มและเราจะเห็นได้ว่าอายุการตายของกษัตริย์อังกฤษดูเหมือนจะเป็นอย่างไร ได้ลดลงจากประมาณ 55 ปีไปประมาณ 38 ปีในรัชสมัยของ 20 กษัตริย์แรกและจากนั้นเพิ่มขึ้นหลังจากที่ไปประมาณ 73 ปีโดยสิ้นรัชกาลของพระเจ้า 40 ในชุดเวลา การแจกแจงข้อมูลตามฤดูกาลซีรีส์เวลาตามฤดูกาลประกอบด้วยองค์ประกอบแนวโน้มองค์ประกอบตามฤดูกาลและองค์ประกอบที่ไม่สม่ำเสมอ การแยกชุดข้อมูลเวลาหมายถึงการแบ่งช่วงเวลาออกเป็นองค์ประกอบสามส่วนนี้นั่นคือการประมาณค่าส่วนประกอบทั้งสามนี้ ในการประมาณองค์ประกอบเทรนด์และองค์ประกอบตามฤดูกาลของซีรีส์เวลาตามฤดูกาลที่สามารถอธิบายได้โดยใช้โมเดล additive เราสามารถใช้ฟังก์ชัน 8220decompose () 8221 ในอาร์ได้ฟังก์ชันนี้จะประมาณการส่วนประกอบตามฤดูกาลและไม่สม่ำเสมอของชุดข้อมูลเวลาที่ สามารถอธิบายได้โดยใช้แบบจำลอง additive 8220decompose ฟังก์ชั่น () 8221 ผลตอบแทนที่วัตถุของรายการที่เป็นผลของมันที่ประมาณการขององค์ประกอบตามฤดูกาลองค์ประกอบแนวโน้มและองค์ประกอบที่ผิดปกติจะถูกเก็บไว้ในองค์ประกอบที่มีชื่อของวัตถุรายการที่เรียกว่า 8220seasonal8221, 8220trend8221 และ 8220random8221 ตามลำดับ ตัวอย่างเช่นตามที่กล่าวไว้ข้างต้นชุดข้อมูลจำนวนเวลาเกิดขึ้นต่อเดือนในนิวยอร์กซิตี้เป็นฤดูกาลที่มียอดทุกฤดูร้อนและร่องน้ำทุกฤดูหนาวและสามารถอธิบายได้โดยใช้แบบจำลอง additive เนื่องจากความผันผวนตามฤดูกาลและแบบสุ่มดูเหมือนจะเป็นไปได้ จะประมาณคงที่ในขนาดที่มากกว่าเวลา: เพื่อประเมินแนวโน้มส่วนประกอบตามฤดูกาลและความผิดปกติของชุดเวลานี้เราพิมพ์: ค่าประมาณของฤดูกาลแนวโน้มและส่วนประกอบที่ผิดปกติในขณะนี้จะถูกเก็บไว้ในตัวแปร birthstimeseriescomponentsseasonal, birthstimeseriescomponentstrend และ birthstimeseriescomponentsrandom ตัวอย่างเช่นเราสามารถพิมพ์ค่าประมาณขององค์ประกอบตามฤดูกาลได้โดยพิมพ์: ปัจจัยฤดูกาลที่คาดการณ์ไว้จะมีขึ้นในช่วงเดือนมกราคม - ธันวาคมและจะเหมือนกันในแต่ละปี ปัจจัยฤดูกาลที่ใหญ่ที่สุดคือเดือนกรกฎาคม (ประมาณ 1.46) และต่ำสุดคือเดือนกุมภาพันธ์ (ประมาณ -2.08) ซึ่งบ่งชี้ว่าดูเหมือนว่าจะมียอดการคลอดสูงสุดในเดือนกรกฎาคมและมีการคลอดในเดือนกุมภาพันธ์ในแต่ละปี เราสามารถจัดทำประมาณการแนวโน้มส่วนประกอบตามฤดูกาลและไม่สม่ำเสมอของชุดข้อมูลเวลาได้โดยใช้ฟังก์ชัน 8220plot () 8221 ตัวอย่างเช่นพล็อตด้านบนแสดงชุดข้อมูลเวลาต้นฉบับ (ด้านบน) องค์ประกอบเทรนด์โดยประมาณ (ที่สองจากด้านบนสุด) องค์ประกอบที่เป็นฤดูกาลตามฤดูกาล (ที่สามจากด้านบน) และองค์ประกอบที่ไม่สม่ำเสมอโดยประมาณ (ด้านล่าง) เราเห็นว่าส่วนประกอบของแนวโน้มที่คาดการณ์ลดลงเล็กน้อยจากประมาณ 24 ในปี 1947 เป็นประมาณ 22 ในปี 1948 ตามมาด้วยการเพิ่มขึ้นอย่างสม่ำเสมอจากนั้นเป็นประมาณ 27 ในปี 1959 การปรับฤดูกาลถ้าคุณมีชุดเวลาตามฤดูกาลที่สามารถอธิบายได้โดยใช้ แบบจำลองเพิ่มเติมคุณสามารถปรับชุดข้อมูลเวลาตามฤดูกาลโดยการประมาณองค์ประกอบตามฤดูกาลและลบส่วนประกอบตามฤดูกาลโดยประมาณออกจากชุดเวลาเดิม เราสามารถทำได้โดยใช้ค่าประมาณขององค์ประกอบตามฤดูกาลที่คำนวณโดยฟังก์ชัน 8221decompose () 8221 ตัวอย่างเช่นหากต้องการปรับฤดูกาลตามจำนวนเดือนเกิดในเมืองนิวยอร์กตามฤดูกาลเราสามารถคาดการณ์ส่วนประกอบตามฤดูกาลโดยใช้ 8220decompose () 8221 จากนั้นลบส่วนประกอบตามฤดูกาลออกจากชุดเวลาเดิม: จากนั้นเราจะสามารถคำนวณ ชุดเวลาที่ปรับฤดูกาลตามฤดูกาลโดยใช้ 8220plot () 8221 ฟังก์ชันโดยพิมพ์: คุณจะเห็นว่ารูปแบบตามฤดูกาลถูกลบออกจากชุดเวลาที่ปรับฤดูกาลแล้ว ซีรีส์เวลาที่ปรับฤดูกาลตามฤดูกาลนี้มีองค์ประกอบเทรนด์และองค์ประกอบที่ไม่สม่ำเสมอเท่านั้น การคาดการณ์โดยใช้ Exponential Smoothing Exponential smoothing สามารถใช้เพื่อคาดการณ์ระยะสั้นสำหรับข้อมูลชุดเวลาได้ การเรียบอย่างง่าย Exponential ถ้าคุณมีชุดข้อมูลเวลาที่สามารถอธิบายได้โดยใช้โมเดล additive ที่มีระดับคงที่และไม่มี seasonality คุณสามารถใช้การคำนวณแบบเลขแจงที่เรียบง่ายเพื่อคาดการณ์ในระยะสั้นได้ วิธีการเรียบง่ายชี้แจงให้เป็นวิธีการประมาณระดับที่จุดเวลาปัจจุบัน Smoothing ถูกควบคุมโดยอัลฟ่าพารามิเตอร์สำหรับการประมาณระดับ ณ จุดเวลาปัจจุบัน ค่าอัลฟาอยู่ระหว่าง 0 ถึง 1. ค่าของอัลฟาที่ใกล้เคียงกับ 0 หมายความว่าน้ำหนักเพียงเล็กน้อยจะอยู่ในข้อสังเกตล่าสุดเมื่อทำการคาดการณ์ค่าในอนาคต ตัวอย่างเช่นไฟล์ robjhyndmantsdldatahurstprecip1.dat มีปริมาณฝนตกทุกปีเป็นนิ้วสำหรับลอนดอนตั้งแต่ปีพ. ศ. 2356 - 2455 (ข้อมูลเดิมจาก Hipel และ McLeod, 1994) เราสามารถอ่านข้อมูลลงใน R และพล็อตได้โดยการพิมพ์: คุณสามารถเห็นได้จากพล็อตที่มีระดับคงที่โดยประมาณ (ค่าคงที่อยู่ที่ประมาณ 25 นิ้ว) ความผันผวนของข้อมูลสุ่มในชุดเวลาดูเหมือนจะมีค่าคงที่โดยประมาณในระยะเวลาหนึ่งดังนั้นจึงอาจเหมาะสมที่จะอธิบายข้อมูลโดยใช้แบบจำลอง additive ดังนั้นเราจึงสามารถคาดการณ์โดยใช้การเรียบอย่างง่ายแทน เพื่อให้การคาดการณ์โดยใช้การเรียบอย่างง่ายใน R เราสามารถใส่คำอธิบายแบบเรียบง่ายที่ทำนายได้ด้วยการใช้ฟังก์ชัน 8220HoltWinters () 8221 ใน R. การใช้ HoltWinters () เพื่อการเรียบอย่างง่ายแทนเราจำเป็นต้องตั้งค่าพารามิเตอร์ betaFALSE และ gammaFALSE ไว้ใน ฟังก์ชั่น HoltWinters () (พารามิเตอร์เบต้าและแกมมาใช้สำหรับการปรับให้เรียบแบบ Holt8217s หรือการเรียบเรียงชี้แจงตาม Holt-Winters ตามที่อธิบายไว้ด้านล่าง) ฟังก์ชัน HoltWinters () จะคืนค่าตัวแปรรายชื่อที่มีองค์ประกอบหลายชื่อ ตัวอย่างเช่นในการใช้การเรียบง่ายแบบเสวนาเพื่อคาดการณ์ปริมาณฝนที่ตกเป็นประจำทุกปีในลอนดอนเราจะพิมพ์: ผลลัพธ์ของ HoltWinters () บอกเราว่าค่าประมาณของพารามิเตอร์ alpha อยู่ที่ประมาณ 0.024 ซึ่งใกล้เคียงกับศูนย์บอกให้เราทราบว่าการคาดการณ์ขึ้นอยู่กับการสังเกตล่าสุดและการสังเกตการณ์ล่าสุดที่ไม่ค่อยได้รับการตอบรับ (แม้ว่าจะมีการสังเกตน้ำหนักที่ค่อนข้างมากก็ตาม) โดยค่าเริ่มต้น HoltWinters () จะทำให้การคาดการณ์ในช่วงเวลาเดียวกันที่ครอบคลุมตามชุดเวลาเดิมของเรา ในกรณีนี้ชุดเวลาเดิมของเรารวมถึงปริมาณน้ำฝนสำหรับลอนดอนจาก 1813-1912 ดังนั้นการคาดการณ์จึงเป็นเช่นนั้นสำหรับ 1813-1912 ในตัวอย่างข้างต้นเราได้เก็บผลลัพธ์ของฟังก์ชัน HoltWinters () ไว้ในตัวแปรรายการ 8220rainseriesforecasts8221 การคาดการณ์ที่ทำโดย HoltWinters () จะถูกเก็บไว้ในองค์ประกอบที่มีชื่อของตัวแปรรายการนี้ชื่อ 8220fitted8221 เพื่อให้เราสามารถรับค่าได้โดยการพิมพ์: เราสามารถพล็อตชุดเวลาต้นฉบับกับการคาดการณ์โดยการพิมพ์: พล็อตแสดงชุดเวลาต้นฉบับใน ดำและการคาดการณ์เป็นเส้นสีแดง ชุดข้อมูลการคาดการณ์ในช่วงเวลานี้ดูเรียบง่ายกว่าชุดข้อมูลเดิมของข้อมูลที่นี่ เพื่อวัดความถูกต้องของการคาดการณ์เราสามารถคำนวณผลรวมของข้อผิดพลาดในการคำนวณสำหรับข้อผิดพลาดในการคาดการณ์ในตัวอย่างนั่นคือข้อผิดพลาดในการคาดการณ์สำหรับช่วงเวลาที่ครอบคลุมตามชุดเวลาเดิมของเรา ข้อผิดพลาดของ sum-of-squared ถูกเก็บไว้ใน element ชื่อของตัวแปร list 8220rainseriesforecasts8221 ที่เรียกว่า 8220SSE8221 เพื่อให้เราสามารถรับค่าได้โดยการพิมพ์: นั่นคือข้อผิดพลาดของ sum-of-squared คือ 1828.855 เป็นเรื่องธรรมดาในการเรียบง่ายชี้แจงให้ใช้ค่าแรกในซีรีส์เวลาเป็นค่าเริ่มต้นสำหรับระดับ ตัวอย่างเช่นในชุดข้อมูลเวลาฝนตกในลอนดอนค่าแรกเป็น 23.56 นิ้วสำหรับปริมาณน้ำฝนในปี 1813 คุณสามารถระบุค่าเริ่มต้นสำหรับระดับในฟังก์ชัน HoltWinters () โดยใช้พารามิเตอร์ 8220l. start8221 ตัวอย่างเช่นเพื่อให้การคาดการณ์ที่มีค่าเริ่มต้นของระดับที่ตั้งไว้ที่ 23.56 เราพิมพ์: ตามที่อธิบายไว้ข้างต้นโดยค่าเริ่มต้น HoltWinters () จะทำให้การคาดการณ์สำหรับช่วงเวลาที่ครอบคลุมโดยข้อมูลเดิมซึ่งเป็น 1813-1912 สำหรับปริมาณน้ำฝน time series เราสามารถคาดการณ์จุดเวลาต่อไปได้โดยใช้ 8220forecast. HoltWinters () 8221 ฟังก์ชันในแพคเกจ R 8220forecast8221 ในการใช้ฟังก์ชัน forecast. HoltWinters () ก่อนอื่นเราต้องติดตั้งแพ็กเกจ 8220forecast8221 R (สำหรับคำแนะนำในการติดตั้งแพคเกจ R โปรดดูที่การติดตั้งแพคเกจ R) เมื่อติดตั้งแพคเกจ 8220forecast8221 R แล้วคุณสามารถโหลดแพคเกจ 8220forecast8221 R ได้โดยการพิมพ์: เมื่อใช้งานฟังก์ชัน forecast. HoltWinters () เป็นอาร์กิวเมนต์แรก (อินพุท) คุณจะส่งผ่านโมเดลการคาดการณ์ที่คุณได้ใช้ไปแล้วโดยใช้ ฟังก์ชัน HoltWinters () ตัวอย่างเช่นในกรณีของชุดข้อมูลปริมาณน้ำฝนเราได้เก็บโมเดลการคาดการณ์ที่ทำโดยใช้ HoltWinters () ในตัวแปร 8220rainseriesforecasts8221 คุณระบุจำนวนจุดเวลาที่คุณต้องการคาดการณ์โดยใช้พารามิเตอร์ 8220h8221 ใน forecast. HoltWinters () ตัวอย่างเช่นเพื่อให้การคาดการณ์ของปริมาณน้ำฝนสำหรับปี 1814-1820 (8 ปีเพิ่มเติม) โดยใช้ forecast. HoltWinters () เราพิมพ์: ฟังก์ชัน forecast. HoltWinters () จะช่วยให้คุณคาดการณ์ปีเป็น 80 ช่วงการคาดการณ์สำหรับ การคาดการณ์และช่วงคาดการณ์ 95 สำหรับการคาดการณ์ ตัวอย่างเช่นปริมาณน้ำฝนที่คาดการณ์ไว้สำหรับปีพ. ศ. 2463 ประมาณ 24.68 นิ้วโดยมีช่วงคาดการณ์ 95 (16.24, 33.11) เราสามารถใช้ 8220plot. forecast () 8221 ฟังก์ชัน: ที่นี่คาดการณ์สำหรับ 1913-1920 ถูกวางแผนเป็นเส้นสีน้ำเงินช่วงการทำนาย 80 เป็นพื้นที่สีส้มสีส้มและ 95 เป็นพื้นที่สีเหลืองที่แรเงา ข้อผิดพลาด 8216forecast ถูกคำนวณเป็นค่าที่สังเกตได้หักค่าที่คาดการณ์ไว้สำหรับแต่ละช่วงเวลา เราสามารถคำนวณข้อผิดพลาดในการคาดการณ์สำหรับช่วงเวลาที่ครอบคลุมตามชุดข้อมูลเดิมของเราซึ่งเป็นข้อมูลปริมาณน้ำฝนในช่วงเวลา 1813-1912 เท่านั้น ดังที่ได้กล่าวมาข้างต้นหนึ่งในการวัดความถูกต้องของรูปแบบการคาดการณ์คือข้อผิดพลาดของ sum-of-squared-errors (SSE) สำหรับข้อผิดพลาดในการคาดการณ์ในตัวอย่าง ข้อผิดพลาดในการคาดการณ์ในตัวอย่างจะถูกเก็บไว้ในองค์ประกอบ 8220 ที่ระบุว่า 82221 ของตัวแปรรายการที่ส่งคืนโดย forecast. HoltWinters () หากไม่สามารถปรับปรุงรูปแบบการคาดการณ์ได้ควรไม่มีความสัมพันธ์ระหว่างข้อผิดพลาดในการคาดการณ์สำหรับการคาดการณ์ต่อเนื่อง กล่าวอีกนัยหนึ่งถ้ามีความสัมพันธ์ระหว่างข้อผิดพลาดในการคาดการณ์สำหรับการคาดการณ์ต่อเนื่องอาจเป็นไปได้ว่าการคาดการณ์การทำให้เรียบแบบเรียบง่ายอาจได้รับการปรับปรุงโดยเทคนิคการพยากรณ์อื่น หากต้องการทราบว่าเป็นกรณีนี้หรือไม่เราสามารถขอรับความผิดพลาดในการคาดการณ์ตัวอย่างในกรณีที่ล่าช้า 1-20 เราสามารถคำนวณ correlogram ของข้อผิดพลาดในการคาดการณ์โดยใช้ฟังก์ชัน 8221a () 8221 ใน R. เพื่อระบุความล่าช้าสูงสุดที่เราต้องการดูเราใช้พารามิเตอร์ 8220lag. max8221 ใน acf () ตัวอย่างเช่นในการคำนวณ correlogram ของข้อผิดพลาดในการคาดการณ์ในตัวอย่างสำหรับข้อมูลปริมาณน้ำฝนในลอนดอนสำหรับความล่าช้า 1-20 เราจะพิมพ์: คุณสามารถดูได้จากตัวอย่าง correlogram ที่ความสัมพันธ์กันที่ระดับ lag 3 เป็นเพียงการสัมผัสความสำคัญเท่านั้น เพื่อทดสอบว่ามีหลักฐานสำคัญสำหรับความสัมพันธ์ที่ไม่ใช่ศูนย์ที่ล่าช้า 1-20 เราสามารถทดสอบ Ljung-Box ได้หรือไม่ ซึ่งสามารถทำได้ใน R โดยใช้ 8220Box. test () 8221 ฟังก์ชัน ความล่าช้าสูงสุดที่เราต้องการดูมีการระบุโดยใช้พารามิเตอร์ 8220lag8221 ในฟังก์ชัน Box. test () ตัวอย่างเช่นเมื่อต้องการทดสอบว่ามีการเชื่อมโยงกันที่ไม่ใช่ศูนย์ที่ล่าช้า 1-20 สำหรับข้อผิดพลาดในการคาดการณ์ตัวอย่างสำหรับข้อมูลปริมาณน้ำฝนในลอนดอนเราจะพิมพ์ที่นี่สถิติทดสอบ Ljung-Box เท่ากับ 17.4 และค่า p คือ 0.6 ดังนั้นมีหลักฐานน้อยมากเกี่ยวกับความสัมพันธ์กันที่ไม่ใช่ศูนย์ในข้อผิดพลาดในการคาดการณ์ในตัวอย่างที่ล่าช้า 1-20 เพื่อให้มั่นใจได้ว่ารูปแบบการคาดการณ์ไม่สามารถปรับปรุงได้ดีกว่าควรเป็นเช่นนี้เพื่อตรวจสอบว่าข้อผิดพลาดในการคาดการณ์มีการแจกแจงแบบปกติหรือไม่และค่าความแปรปรวนคงที่ เพื่อตรวจสอบว่าข้อผิดพลาดในการคาดการณ์มีความแปรปรวนคงที่เราสามารถทำพล็อตเวลาของข้อผิดพลาดในการคาดการณ์ในตัวอย่าง: พล็อตแสดงให้เห็นว่าข้อผิดพลาดในการคาดการณ์ในตัวอย่างดูเหมือนจะมีความแปรปรวนคงที่ประมาณตลอดเวลาแม้ว่าจะมีขนาดของความผันผวนใน เริ่มต้นของชุดเวลา (1820-1830) อาจจะน้อยกว่าเล็กน้อยในภายหลัง (เช่น 1840-1850) เพื่อตรวจสอบว่าข้อผิดพลาดในการคาดการณ์มีการแจกจ่ายโดยปกติหรือไม่ศูนย์เราสามารถจัดทำฮิสโตแกรมของข้อผิดพลาดในการคาดการณ์ได้โดยมีเส้นกราฟปกติที่ซ้อนทับซึ่งมีค่าเฉลี่ยศูนย์และมีค่าเบี่ยงเบนมาตรฐานเท่ากับการแจกแจงข้อผิดพลาดในการคาดการณ์ ในการทำเช่นนี้เราสามารถกำหนดฟังก์ชัน R 8220plotForecastErrors () 8221 ด้านล่าง: คุณจะต้องคัดลอกฟังก์ชันข้างต้นลงใน R เพื่อใช้งาน จากนั้นคุณสามารถใช้ plotForecastErrors () เพื่อทำกราฟฮิสโตแกรม (มีการทับระดับเส้นโค้งปกติ) ของข้อผิดพลาดในการคาดการณ์สำหรับการคาดการณ์ปริมาณน้ำฝน: พล็อตแสดงว่าการแจกแจงข้อผิดพลาดในการคาดการณ์นั้นอยู่ตรงกลางเป็นศูนย์และมีการกระจายตามปกติมากกว่าหรือน้อยกว่า ดูเหมือนว่าจะเบี่ยงเบนไปทางขวาเล็กน้อยเมื่อเทียบกับเส้นโค้งปกติ อย่างไรก็ตามความลาดเอียงขวาค่อนข้างเล็กและดังนั้นจึงเป็นไปได้ที่ข้อผิดพลาดในการคาดการณ์จะมีการกระจายตามปกติโดยมีค่าเฉลี่ยศูนย์ การทดสอบ Ljung-Box แสดงให้เห็นว่ามีหลักฐานน้อยมากเกี่ยวกับการสัมพันธ์กันที่ไม่ใช่ศูนย์ในข้อผิดพลาดในการคาดการณ์ในตัวอย่างและการแจกแจงข้อผิดพลาดในการคาดการณ์ดูเหมือนว่าจะมีการกระจายตามปกติโดยมีค่าเฉลี่ยเป็นศูนย์ นี่แสดงให้เห็นว่าวิธีการเรียบง่ายชี้แจงให้รูปแบบการคาดการณ์ที่เพียงพอสำหรับปริมาณน้ำฝนในลอนดอนซึ่งอาจจะไม่สามารถปรับปรุงให้ดีขึ้นได้ นอกจากนี้สมมติฐานที่ว่าช่วงการคาดการณ์ 80 และ 95 ขึ้นอยู่กับ (ที่ไม่มีการเทียบอัตโนมัติในข้อผิดพลาดในการคาดการณ์และข้อผิดพลาดในการคาดการณ์จะมีการกระจายตามปกติโดยมีค่าเฉลี่ยศูนย์และค่าความแปรปรวนคงที่) อาจถูกต้อง ถ้าคุณมีชุดข้อมูลเวลาที่สามารถอธิบายได้โดยใช้แบบจำลอง additive ที่มีแนวโน้มเพิ่มขึ้นหรือลดลงและไม่มีฤดูกาลคุณสามารถใช้การคำนวณหากำไรแบบละเอียดเพื่อให้ได้การคาดการณ์ในระยะสั้น Holt8217s การคำนวณความล้าสมัยของ Holt8217s จะประมาณระดับและความลาดชัน ณ จุดเวลาปัจจุบัน Smoothing ถูกควบคุมโดยพารามิเตอร์สองค่าอัลฟ่าสำหรับการประมาณระดับ ณ จุดเวลาปัจจุบันและเบต้าสำหรับการประมาณความชันของส่วนประกอบแนวโน้มที่จุดเวลาปัจจุบัน เช่นเดียวกับการเรียบง่ายชี้แจงอัลฟ่าและเบต้า paramters มีค่าระหว่าง 0 ถึง 1 และค่าที่ใกล้เคียงกับ 0 หมายความว่าน้ำหนักเพียงเล็กน้อยจะถูกวางไว้กับข้อสังเกตล่าสุดเมื่อทำการคาดการณ์ค่าในอนาคต ตัวอย่างชุดเวลาที่สามารถอธิบายได้โดยใช้แบบจำลองเพิ่มเติมที่มีแนวโน้มและไม่มีฤดูกาลเป็นชุดข้อมูลขนาดเส้นผ่านศูนย์กลางของผู้หญิงในช่วงปีพ. ศ. 2409 ถึง พ. ศ. 2454 มีข้อมูลอยู่ในไฟล์ robjhyndmantsdldatarobertsskirts dat (ข้อมูลต้นฉบับจาก Hipel และ McLeod, 1994) เราสามารถอ่านและวางแผนข้อมูลใน R โดยการพิมพ์: เราสามารถดูได้จากพล็อตว่ามีเส้นผ่านศูนย์กลางของเส้นด้ายเพิ่มขึ้นจากประมาณ 600 ใน 1866 ถึงประมาณ 1050 ในปี 1880 และหลังจากนั้นเส้นผ่านศูนย์กลางของห้อยลดลงประมาณ 520 ในปี 1911 . ในการทำให้การคาดการณ์ของเราสามารถให้พอดีกับรูปแบบการทำนายโดยใช้ HoltWinters () ฟังก์ชันในอาร์ในการใช้ HoltWinters () เพื่อ Holt8217s เรียบชี้แจงเราจำเป็นต้องตั้งค่าพารามิเตอร์ gammaFALSE (พารามิเตอร์แกมมาที่ใช้สำหรับการ HoltWinters ชี้แจงเรียบ ตามที่อธิบายไว้ด้านล่าง) ตัวอย่างเช่นในการใช้การอธิบายแบบย่อหน้า Holt8217s ให้พอดีกับรูปแบบการคาดการณ์สำหรับเส้นผ่าศูนย์กลางของกระโปรงเส้นผ่าศูนย์กลางเราพิมพ์: ค่าประมาณของ alpha เท่ากับ 0.84 และ beta คือ 1.00 ค่าเหล่านี้สูงทั้งสองบอกเราว่าการประมาณค่าปัจจุบันของระดับและความชันขององค์ประกอบแนวโน้มจะขึ้นอยู่กับข้อสังเกตล่าสุดในชุดข้อมูลเวลา นี้ทำให้รู้สึกดีใช้งานง่ายเนื่องจากระดับและความลาดเอียงของชุดเวลาทั้งสองเปลี่ยนแปลงได้ค่อนข้างมากเมื่อเวลาผ่านไป ค่าของข้อผิดพลาดในการประมาณค่าของข้อผิดพลาดในการประมาณตัวอย่างคือ 16954 เราสามารถคำนวณชุดข้อมูลเวลาต้นฉบับเป็นเส้นสีดำโดยมีค่าที่คาดการณ์เป็นเส้นสีแดงอยู่ด้านบนโดยพิมพ์: เรา สามารถมองเห็นได้จากภาพที่คาดการณ์ในตัวอย่างดีกว่าค่าที่สังเกตได้แม้ว่าจะมีแนวโน้มที่จะล้าหลังค่าที่สังเกตได้นิดหน่อย ถ้าคุณต้องการคุณสามารถระบุค่าเริ่มต้นของระดับและความชันของคอมโพเนนต์แนวโน้มโดยใช้อาร์กิวเมนต์ 8220l. start8221 และ 8220b. start8221 สำหรับฟังก์ชัน HoltWinters () โดยปกติแล้วจะกำหนดค่าเริ่มต้นของระดับเป็นค่าแรกในชุดข้อมูลเวลา (608 สำหรับข้อมูลกระโปรง) และค่าเริ่มต้นของความชันเป็นค่าที่สองซึ่งลบด้วยค่าแรก (9 สำหรับข้อมูลกระโปรง) ยกตัวอย่างเช่นเพื่อให้พอดีกับรูปแบบการทำนายของข้อมูลที่อยู่ในกระโปรงโดยใช้การคำนวณหาค่าความละเอียดแบบละเอียดแบบ Holt8217s โดยมีค่าเริ่มต้น 608 สำหรับระดับและ 9 สำหรับความลาดชัน b ขององค์ประกอบแนวโน้มเราพิมพ์: สำหรับการเรียบง่ายชี้แจงเราสามารถคาดการณ์ได้ สำหรับเวลาในอนาคตที่ไม่ได้ครอบคลุมตามชุดเวลาเดิมโดยใช้ฟังก์ชัน forecast. HoltWinters () ในชุด 8220forecast8221 ตัวอย่างเช่นข้อมูลชุดข้อมูลเวลาของเราสำหรับชุดกระโปรงเป็นช่วงปี 1866 ถึง 1911 ทำให้เราสามารถคาดการณ์ได้ตั้งแต่ 1912 ถึง 1930 (19 จุดข้อมูลเพิ่มเติม) และวางแผนโดยพิมพ์: การคาดการณ์จะแสดงเป็นเส้นสีน้ำเงินโดยมี 80 ช่วงการคาดการณ์เป็นพื้นที่สีส้มที่เป็นสีส้มและช่วงคาดการณ์ 95 เป็นพื้นที่สีเหลืองที่แรเงา สำหรับการเรียบง่ายชี้แจงเราสามารถตรวจสอบว่ารูปแบบการทำนายสามารถปรับปรุงได้โดยการตรวจสอบว่าข้อผิดพลาดในการคาดการณ์ตัวอย่างแสดง autocorrelations ที่ไม่เป็นศูนย์ที่ lags 1-20 ตัวอย่างเช่นสำหรับข้อมูลของชุดกระโปรงเราสามารถสร้าง correlogram และดำเนินการทดสอบ Ljung-Box โดยการพิมพ์: ที่นี่ correlogram แสดงให้เห็นว่าตัวอย่างความสัมพันธ์อัตโนมัติสำหรับข้อผิดพลาดในการคาดการณ์ในตัวอย่างที่ระดับ lag 5 เกินขอบเขตที่มีนัยสำคัญ อย่างไรก็ตามเราคาดว่าหนึ่งใน 20 ของ autocorrelations สำหรับ 20 ครั้งแรกล่าช้าเกินขอบเขตสำคัญ 95 โดยบังเอิญเพียงอย่างเดียว เมื่อเราดำเนินการทดสอบ Ljung-Box ค่า p จะเท่ากับ 0.47 ซึ่งแสดงให้เห็นว่ามีหลักฐานน้อยมากเกี่ยวกับความสัมพันธ์กันที่ไม่ใช่ศูนย์ในข้อผิดพลาดในการคาดการณ์ในตัวอย่างที่ล่าช้า 1-20 สำหรับการเรียบเรียบง่ายเราควรตรวจสอบว่าข้อผิดพลาดในการคาดการณ์มีความแปรปรวนคงที่ตลอดเวลาและมีการกระจายตามปกติโดยมีค่าเฉลี่ยเป็นศูนย์ เราสามารถทำได้โดยทำพล็อตเวลาของข้อผิดพลาดในการคาดการณ์และฮิสโตแกรมของการแจกแจงข้อผิดพลาดในการคาดการณ์กับเส้นโค้งปกติที่ซ้อนทับ: พล็อตเวลาของข้อผิดพลาดในการคาดการณ์แสดงให้เห็นว่าข้อผิดพลาดในการคาดการณ์มีความแปรปรวนคงที่ประมาณตลอดเวลา ฮิสโทแกรมของข้อผิดพลาดในการคาดการณ์แสดงให้เห็นว่าเป็นไปได้ว่าข้อผิดพลาดในการคาดการณ์มีการกระจายตามปกติโดยค่าเฉลี่ยศูนย์และความแปรปรวนคงที่ ดังนั้นการทดสอบ Ljung-Box แสดงให้เห็นว่ามีหลักฐานน้อยมากเกี่ยวกับความคลาดเคลื่อนในข้อผิดพลาดในการคาดการณ์ขณะที่พล็อตเวลาและฮิสโตแกรมของข้อผิดพลาดในการคาดการณ์แสดงให้เห็นว่าเป็นไปได้ว่าข้อผิดพลาดในการคาดการณ์มีการแจกแจงแบบปกติด้วยค่าเฉลี่ยศูนย์และความแปรปรวนคงที่ ดังนั้นเราจึงสามารถสรุปได้ว่าการคำนวณหาค่าความละเอียดแบบเอกซ์ไทม์ Holt8217s ให้รูปแบบการทำนายที่เพียงพอสำหรับเส้นผ่าศูนย์กลางของกระโปรงซึ่งอาจไม่สามารถปรับปรุงได้ นอกจากนี้ยังหมายถึงสมมติฐานที่ว่าช่วงการคาดการณ์ 80 และ 95 ขึ้นอยู่กับว่าอาจเป็นผล Holt-Winters Exponential Smoothing If you have a time series that can be described using an additive model with increasing or decreasing trend and seasonality, you can use Holt-Winters exponential smoothing to make short-term forecasts. Holt-Winters exponential smoothing estimates the level, slope and seasonal component at the current time point. Smoothing is controlled by three parameters: alpha, beta, and gamma, for the estimates of the level, slope b of the trend component, and the seasonal component, respectively, at the current time point. The parameters alpha, beta and gamma all have values between 0 and 1, and values that are close to 0 mean that relatively little weight is placed on the most recent observations when making forecasts of future values. An example of a time series that can probably be described using an additive model with a trend and seasonality is the time series of the log of monthly sales for the souvenir shop at a beach resort town in Queensland, Australia (discussed above): To make forecasts, we can fit a predictive model using the HoltWinters() function. For example, to fit a predictive model for the log of the monthly sales in the souvenir shop, we type: The estimated values of alpha, beta and gamma are 0.41, 0.00, and 0.96, respectively. The value of alpha (0.41) is relatively low, indicating that the estimate of the level at the current time point is based upon both recent observations and some observations in the more distant past. The value of beta is 0.00, indicating that the estimate of the slope b of the trend component is not updated over the time series, and instead is set equal to its initial value. This makes good intuitive sense, as the level changes quite a bit over the time series, but the slope b of the trend component remains roughly the same. In contrast, the value of gamma (0.96) is high, indicating that the estimate of the seasonal component at the current time point is just based upon very recent observations. As for simple exponential smoothing and Holt8217s exponential smoothing, we can plot the original time series as a black line, with the forecasted values as a red line on top of that: We see from the plot that the Holt-Winters exponential method is very successful in predicting the seasonal peaks, which occur roughly in November every year. To make forecasts for future times not included in the original time series, we use the 8220forecast. HoltWinters()8221 function in the 8220forecast8221 package. For example, the original data for the souvenir sales is from January 1987 to December 1993. If we wanted to make forecasts for January 1994 to December 1998 (48 more months), and plot the forecasts, we would type: The forecasts are shown as a blue line, and the orange and yellow shaded areas show 80 and 95 prediction intervals, respectively. We can investigate whether the predictive model can be improved upon by checking whether the in-sample forecast errors show non-zero autocorrelations at lags 1-20, by making a correlogram and carrying out the Ljung-Box test: The correlogram shows that the autocorrelations for the in-sample forecast errors do not exceed the significance bounds for lags 1-20. Furthermore, the p-value for Ljung-Box test is 0.6, indicating that there is little evidence of non-zero autocorrelations at lags 1-20. We can check whether the forecast errors have constant variance over time, and are normally distributed with mean zero, by making a time plot of the forecast errors and a histogram (with overlaid normal curve): From the time plot, it appears plausible that the forecast errors have constant variance over time. From the histogram of forecast errors, it seems plausible that the forecast errors are normally distributed with mean zero. Thus, there is little evidence of autocorrelation at lags 1-20 for the forecast errors, and the forecast errors appear to be normally distributed with mean zero and constant variance over time. This suggests that Holt-Winters exponential smoothing provides an adequate predictive model of the log of sales at the souvenir shop, which probably cannot be improved upon. Furthermore, the assumptions upon which the prediction intervals were based are probably valid. ARIMA Models Exponential smoothing methods are useful for making forecasts, and make no assumptions about the correlations between successive values of the time series. However, if you want to make prediction intervals for forecasts made using exponential smoothing methods, the prediction intervals require that the forecast errors are uncorrelated and are normally distributed with mean zero and constant variance. While exponential smoothing methods do not make any assumptions about correlations between successive values of the time series, in some cases you can make a better predictive model by taking correlations in the data into account. Autoregressive Integrated Moving Average (ARIMA) models include an explicit statistical model for the irregular component of a time series, that allows for non-zero autocorrelations in the irregular component. Differencing a Time Series ARIMA models are defined for stationary time series. Therefore, if you start off with a non-stationary time series, you will first need to 8216difference8217 the time series until you obtain a stationary time series. If you have to difference the time series d times to obtain a stationary series, then you have an ARIMA(p, d,q) model, where d is the order of differencing used. You can difference a time series using the 8220diff()8221 function in R. For example, the time series of the annual diameter of women8217s skirts at the hem, from 1866 to 1911 is not stationary in mean, as the level changes a lot over time: We can difference the time series (which we stored in 8220skirtsseries8221, see above) once, and plot the differenced series, by typing: The resulting time series of first differences (above) does not appear to be stationary in mean. Therefore, we can difference the time series twice, to see if that gives us a stationary time series: Formal tests for stationarity Formal tests for stationarity called 8220unit root tests8221 are available in the fUnitRoots package, available on CRAN, but will not be discussed here. The time series of second differences (above) does appear to be stationary in mean and variance, as the level of the series stays roughly constant over time, and the variance of the series appears roughly constant over time. Thus, it appears that we need to difference the time series of the diameter of skirts twice in order to achieve a stationary series. If you need to difference your original time series data d times in order to obtain a stationary time series, this means that you can use an ARIMA(p, d,q) model for your time series, where d is the order of differencing used. For example, for the time series of the diameter of women8217s skirts, we had to difference the time series twice, and so the order of differencing (d) is 2. This means that you can use an ARIMA(p,2,q) model for your time series. The next step is to figure out the values of p and q for the ARIMA model. Another example is the time series of the age of death of the successive kings of England (see above): From the time plot (above), we can see that the time series is not stationary in mean. To calculate the time series of first differences, and plot it, we type: The time series of first differences appears to be stationary in mean and variance, and so an ARIMA(p,1,q) model is probably appropriate for the time series of the age of death of the kings of England. By taking the time series of first differences, we have removed the trend component of the time series of the ages at death of the kings, and are left with an irregular component. We can now examine whether there are correlations between successive terms of this irregular component if so, this could help us to make a predictive model for the ages at death of the kings. Selecting a Candidate ARIMA Model If your time series is stationary, or if you have transformed it to a stationary time series by differencing d times, the next step is to select the appropriate ARIMA model, which means finding the values of most appropriate values of p and q for an ARIMA(p, d,q) model. To do this, you usually need to examine the correlogram and partial correlogram of the stationary time series. To plot a correlogram and partial correlogram, we can use the 8220acf()8221 and 8220pacf()8221 functions in R, respectively. To get the actual values of the autocorrelations and partial autocorrelations, we set 8220plotFALSE8221 in the 8220acf()8221 and 8220pacf()8221 functions. Example of the Ages at Death of the Kings of England For example, to plot the correlogram for lags 1-20 of the once differenced time series of the ages at death of the kings of England, and to get the values of the autocorrelations, we type: We see from the correlogram that the autocorrelation at lag 1 (-0.360) exceeds the significance bounds, but all other autocorrelations between lags 1-20 do not exceed the significance bounds. To plot the partial correlogram for lags 1-20 for the once differenced time series of the ages at death of the English kings, and get the values of the partial autocorrelations, we use the 8220pacf()8221 function, by typing: The partial correlogram shows that the partial autocorrelations at lags 1, 2 and 3 exceed the significance bounds, are negative, and are slowly decreasing in magnitude with increasing lag (lag 1: -0.360, lag 2: -0.335, lag 3:-0.321). The partial autocorrelations tail off to zero after lag 3. Since the correlogram is zero after lag 1, and the partial correlogram tails off to zero after lag 3, this means that the following ARMA (autoregressive moving average) models are possible for the time series of first differences: an ARMA(3,0) model, that is, an autoregressive model of order p3, since the partial autocorrelogram is zero after lag 3, and the autocorrelogram tails off to zero (although perhaps too abruptly for this model to be appropriate) an ARMA(0,1) model, that is, a moving average model of order q1, since the autocorrelogram is zero after lag 1 and the partial autocorrelogram tails off to zero an ARMA(p, q) model, that is, a mixed model with p and q greater than 0, since the autocorrelogram and partial correlogram tail off to zero (although the correlogram probably tails off to zero too abruptly for this model to be appropriate) We use the principle of parsimony to decide which model is best: that is, we assum e that the model with the fewest parameters is best. The ARMA(3,0) model has 3 parameters, the ARMA(0,1) model has 1 parameter, and the ARMA(p, q) model has at least 2 parameters. Therefore, the ARMA(0,1) model is taken as the best model. An ARMA(0,1) model is a moving average model of order 1, or MA(1) model. This model can be written as: Xt - mu Zt - (theta Zt-1), where Xt is the stationary time series we are studying (the first differenced series of ages at death of English kings), mu is the mean of time series Xt, Zt is white noise with mean zero and constant variance, and theta is a parameter that can be estimated. A MA (moving average) model is usually used to model a time series that shows short-term dependencies between successive observations. Intuitively, it makes good sense that a MA model can be used to describe the irregular component in the time series of ages at death of English kings, as we might expect the age at death of a particular English king to have some effect on the ages at death of the next king or two, but not much effect on the ages at death of kings that reign much longer after that. Shortcut: the auto. arima() function The auto. arima() function can be used to find the appropriate ARIMA model, eg. type 8220library(forecast)8221, then 8220auto. arima(kings)8221. The output says an appropriate model is ARIMA(0,1,1). Since an ARMA(0,1) model (with p0, q1) is taken to be the best candidate model for the time series of first differences of the ages at death of English kings, then the original time series of the ages of death can be modelled using an ARIMA(0,1,1) model (with p0, d1, q1, where d is the order of differencing required). Example of the Volcanic Dust Veil in the Northern Hemisphere Let8217s take another example of selecting an appropriate ARIMA model. The file file robjhyndmantsdldataannualdvi. dat contains data on the volcanic dust veil index in the northern hemisphere, from 1500-1969 (original data from Hipel and Mcleod, 1994). This is a measure of the impact of volcanic eruptions8217 release of dust and aerosols into the environment. We can read it into R and make a time plot by typing: From the time plot, it appears that the random fluctuations in the time series are roughly constant in size over time, so an additive model is probably appropriate for describing this time series. Furthermore, the time series appears to be stationary in mean and variance, as its level and variance appear to be roughly constant over time. Therefore, we do not need to difference this series in order to fit an ARIMA model, but can fit an ARIMA model to the original series (the order of differencing required, d, is zero here). We can now plot a correlogram and partial correlogram for lags 1-20 to investigate what ARIMA model to use: We see from the correlogram that the autocorrelations for lags 1, 2 and 3 exceed the significance bounds, and that the autocorrelations tail off to zero after lag 3. The autocorrelations for lags 1, 2, 3 are positive, and decrease in magnitude with increasing lag (lag 1: 0.666, lag 2: 0.374, lag 3: 0.162). The autocorrelation for lags 19 and 20 exceed the significance bounds too, but it is likely that this is due to chance, since they just exceed the significance bounds (especially for lag 19), the autocorrelations for lags 4-18 do not exceed the signifiance bounds, and we would expect 1 in 20 lags to exceed the 95 significance bounds by chance alone. From the partial autocorrelogram, we see that the partial autocorrelation at lag 1 is positive and exceeds the significance bounds (0.666), while the partial autocorrelation at lag 2 is negative and also exceeds the significance bounds (-0.126). The partial autocorrelations tail off to zero after lag 2. Since the correlogram tails off to zero after lag 3, and the partial correlogram is zero after lag 2, the following ARMA models are possible for the time series: an ARMA(2,0) model, since the partial autocorrelogram is zero after lag 2, and the correlogram tails off to zero after lag 3, and the partial correlogram is zero after lag 2 an ARMA(0,3) model, since the autocorrelogram is zero after lag 3, and the partial correlogram tails off to zero (although perhaps too abruptly for this model to be appropriate) an ARMA(p, q) mixed model, since the correlogram and partial correlogram tail off to zero (although the partial correlogram perhaps tails off too abruptly for this model to be appropriate) Shortcut: the auto. arima() function Again, we can use auto. arima() to find an appropriate model, by typing 8220auto. arima(volcanodust)8221, which gives us ARIMA(1,0,2), which has 3 parameters. However, different criteria can be used to select a model (see auto. arima() help page). If we use the 8220bic8221 criterion, which penalises the number of parameters, we get ARIMA(2,0,0), which is ARMA(2,0): 8220auto. arima(volcanodust, ic8221bic8221)8221. The ARMA(2,0) model has 2 parameters, the ARMA(0,3) model has 3 parameters, and the ARMA(p, q) model has at least 2 parameters. Therefore, using the principle of parsimony, the ARMA(2,0) model and ARMA(p, q) model are equally good candidate models. An ARMA(2,0) model is an autoregressive model of order 2, or AR(2) model. This model can be written as: Xt - mu (Beta1 (Xt-1 - mu)) (Beta2 (Xt-2 - mu)) Zt, where Xt is the stationary time series we are studying (the time series of volcanic dust veil index), mu is the mean of time series Xt, Beta1 and Beta2 are parameters to be estimated, and Zt is white noise with mean zero and constant variance. An AR (autoregressive) model is usually used to model a time series which shows longer term dependencies between successive observations. Intuitively, it makes sense that an AR model could be used to describe the time series of volcanic dust veil index, as we would expect volcanic dust and aerosol levels in one year to affect those in much later years, since the dust and aerosols are unlikely to disappear quickly. If an ARMA(2,0) model (with p2, q0) is used to model the time series of volcanic dust veil index, it would mean that an ARIMA(2,0,0) model can be used (with p2, d0, q0, where d is the order of differencing required). Similarly, if an ARMA(p, q) mixed model is used, where p and q are both greater than zero, than an ARIMA(p,0,q) model can be used. Forecasting Using an ARIMA Model Once you have selected the best candidate ARIMA(p, d,q) model for your time series data, you can estimate the parameters of that ARIMA model, and use that as a predictive model for making forecasts for future values of your time series. You can estimate the parameters of an ARIMA(p, d,q) model using the 8220arima()8221 function in R. Example of the Ages at Death of the Kings of England For example, we discussed above that an ARIMA(0,1,1) model seems a plausible model for the ages at deaths of the kings of England. You can specify the values of p, d and q in the ARIMA model by using the 8220order8221 argument of the 8220arima()8221 function in R. To fit an ARIMA(p, d,q) model to this time series (which we stored in the variable 8220kingstimeseries8221, see above), we type: As mentioned above, if we are fitting an ARIMA(0,1,1) model to our time series, it means we are fitting an an ARMA(0,1) model to the time series of first differences. An ARMA(0,1) model can be written Xt - mu Zt - (theta Zt-1), where theta is a parameter to be estimated. From the output of the 8220arima()8221 R function (above), the estimated value of theta (given as 8216ma18217 in the R output) is -0.7218 in the case of the ARIMA(0,1,1) model fitted to the time series of ages at death of kings. Specifying the confidence level for prediction intervals You can specify the confidence level for prediction intervals in forecast. Arima() by using the 8220level8221 argument. For example, to get a 99.5 prediction interval, we would type 8220forecast. Arima(kingstimeseriesarima, h5, levelc(99.5))8221. We can then use the ARIMA model to make forecasts for future values of the time series, using the 8220forecast. Arima()8221 function in the 8220forecast8221 R package. For example, to forecast the ages at death of the next five English kings, we type: The original time series for the English kings includes the ages at death of 42 English kings. The forecast. Arima() function gives us a forecast of the age of death of the next five English kings (kings 43-47), as well as 80 and 95 prediction intervals for those predictions. The age of death of the 42nd English king was 56 years (the last observed value in our time series), and the ARIMA model gives the forecasted age at death of the next five kings as 67.8 years. We can plot the observed ages of death for the first 42 kings, as well as the ages that would be predicted for these 42 kings and for the next 5 kings using our ARIMA(0,1,1) model, by typing: As in the case of exponential smoothing models, it is a good idea to investigate whether the forecast errors of an ARIMA model are normally distributed with mean zero and constant variance, and whether the are correlations between successive forecast errors. For example, we can make a correlogram of the forecast errors for our ARIMA(0,1,1) model for the ages at death of kings, and perform the Ljung-Box test for lags 1-20, by typing: Since the correlogram shows that none of the sample autocorrelations for lags 1-20 exceed the significance bounds, and the p-value for the Ljung-Box test is 0.9, we can conclude that there is very little evidence for non-zero autocorrelations in the forecast errors at lags 1-20. To investigate whether the forecast errors are normally distributed with mean zero and constant variance, we can make a time plot and histogram (with overlaid normal curve) of the forecast errors: The time plot of the in-sample forecast errors shows that the variance of the forecast errors seems to be roughly constant over time (though perhaps there is slightly higher variance for the second half of the time series). The histogram of the time series shows that the forecast errors are roughly normally distributed and the mean seems to be close to zero. Therefore, it is plausible that the forecast errors are normally distributed with mean zero and constant variance. Since successive forecast errors do not seem to be correlated, and the forecast errors seem to be normally distributed with mean zero and constant variance, the ARIMA(0,1,1) does seem to provide an adequate predictive model for the ages at death of English kings. Example of the Volcanic Dust Veil in the Northern Hemisphere We discussed above that an appropriate ARIMA model for the time series of volcanic dust veil index may be an ARIMA(2,0,0) model. To fit an ARIMA(2,0,0) model to this time series, we can type: As mentioned above, an ARIMA(2,0,0) model can be written as: written as: Xt - mu (Beta1 (Xt-1 - mu)) (Beta2 (Xt-2 - mu)) Zt, where Beta1 and Beta2 are parameters to be estimated. The output of the arima() function tells us that Beta1 and Beta2 are estimated as 0.7533 and -0.1268 here (given as ar1 and ar2 in the output of arima()). Now we have fitted the ARIMA(2,0,0) model, we can use the 8220forecast. ARIMA()8221 model to predict future values of the volcanic dust veil index. The original data includes the years 1500-1969. To make predictions for the years 1970-2000 (31 more years), we type: We can plot the original time series, and the forecasted values, by typing: One worrying thing is that the model has predicted negative values for the volcanic dust veil index, but this variable can only have positive values The reason is that the arima() and forecast. Arima() functions don8217t know that the variable can only take positive values. Clearly, this is not a very desirable feature of our current predictive model. Again, we should investigate whether the forecast errors seem to be correlated, and whether they are normally distributed with mean zero and constant variance. To check for correlations between successive forecast errors, we can make a correlogram and use the Ljung-Box test: The correlogram shows that the sample autocorrelation at lag 20 exceeds the significance bounds. However, this is probably due to chance, since we would expect one out of 20 sample autocorrelations to exceed the 95 significance bounds. Furthermore, the p-value for the Ljung-Box test is 0.2, indicating that there is little evidence for non-zero autocorrelations in the forecast errors for lags 1-20. To check whether the forecast errors are normally distributed with mean zero and constant variance, we make a time plot of the forecast errors, and a histogram: The time plot of forecast errors shows that the forecast errors seem to have roughly constant variance over time. However, the time series of forecast errors seems to have a negative mean, rather than a zero mean. We can confirm this by calculating the mean forecast error, which turns out to be about -0.22: The histogram of forecast errors (above) shows that although the mean value of the forecast errors is negative, the distribution of forecast errors is skewed to the right compared to a normal curve. Therefore, it seems that we cannot comfortably conclude that the forecast errors are normally distributed with mean zero and constant variance Thus, it is likely that our ARIMA(2,0,0) model for the time series of volcanic dust veil index is not the best model that we could make, and could almost definitely be improved upon Links and Further Reading Here are some links for further reading. For a more in-depth introduction to R, a good online tutorial is available on the 8220Kickstarting R8221 website, cran. r-project. orgdoccontribLemon-kickstart . There is another nice (slightly more in-depth) tutorial to R available on the 8220Introduction to R8221 website, cran. r-project. orgdocmanualsR-intro. html . You can find a list of R packages for analysing time series data on the CRAN Time Series Task View webpage . To learn about time series analysis, I would highly recommend the book 8220Time series8221 (product code M24902) by the Open University, available from the Open University Shop . There are two books available in the 8220Use R8221 series on using R for time series analyses, the first is Introductory Time Series with R by Cowpertwait and Metcalfe, and the second is Analysis of Integrated and Cointegrated Time Series with R by Pfaff. Acknowledgements I am grateful to Professor Rob Hyndman. for kindly allowing me to use the time series data sets from his Time Series Data Library (TSDL) in the examples in this booklet. Many of the examples in this booklet are inspired by examples in the excellent Open University book, 8220Time series8221 (product code M24902), available from the Open University Shop . Thank you to Ravi Aranke for bringing auto. arima() to my attention, and Maurice Omane-Adjepong for bringing unit root tests to my attention, and Christian Seubert for noticing a small bug in plotForecastErrors(). Thank you for other comments to Antoine Binard and Bill Johnston. I will be grateful if you will send me (Avril Coghlan) corrections or suggestions for improvements to my email address alc 64 sanger 46 ac 46 uk

Comments