Write R functions for forecasting future values

Assignment Help Programming Languages
Reference no: EM131319127

In this assignment, you will write R functions for forecasting future values of a time series, and apply them to observations on numbers of deaths and maximum temperatures in Houston, Texas. Doing this will provide more practice in basic R programming, and on the use of data frames and of subscripts that are numeric or logical vectors.

The data is derived from that distributed by the U.S. National Morbidity Mortality Air Pollution Study, NMMAPS, with some strangely missing temperature values filled in by reference to www.wunderground.com. I have provided data from 1994-01-01 to 2000-12-31 as a file on the course web page, which may be read as a data frame as follows:

houston <- read.table("https://www.cs.utoronto.ca/~radford/csc120/houston",head=TRUE)

The rows of this data frame have data for successive days, with the row names being the dates. The column names are as follows:

deaths                Number of deaths in Houston that day

tmax                   Maximum temperature that day, in degrees Farenheit

day_of_year       Day of the year, starting at 1 for January 1

day_of_week      Day of the week, 1 to 7, with  1=Sunday

month                 Month day is in, 1 to 12, with  1=January

We wish to forecast the number of deaths and the maximum temperature for each day, based only on data before that day, except that when forecasting the number of deaths on a day, we may use the maximum temperature for that day, as well as previous days. (Of course, if we actually wished to forecast the number of deaths beforehand using the same day's maximum temperature, we'd have to substitute a temperature forecast for the actual temperature, but we won't worry about that for this assignment.)

Once you have produced forecasts for each day, you will produce plots of the forecasts, the actual values, and the errors in the forecasts. You will also evaluate how good these forecasts were in terms of the average absolute value of the error.

You should write several functions for forecasting the value of some variable on a single day. All these functions should take as their first arguments a data frame, containing variables that may be used in making the forecast, and as their second argument a series of past values for the variable being forecast (which will have at least one past value, and for some functions will have to have more than one past value). This forecasting function should return a forecast for the next value in this series. The data frame will have at least as many rows as the length of the series plus one (so there will be values for the day for which the forecast is being made). The data frame may have additional rows, but they should not be looked at when making the forecast for this day.

You should write several forecasting functions of this sort, which make forecasts as follows:

forecast_previous                            The value for the immediately previous day

forecast_week_ago                         The value seven days previous

forecast_mean                                 The average of all previous values

forecast_mean14                             The average of the values on the 14 previous days

forecast_same_day_of_week          Average of previous days that are the same day of the week

forecast_same_month                     Average of previous days that are the same month

forecast_similar_tmax                     For forecasting deaths only. The average of previous days for which the maximum temperature was no more than 4 degrees different from the maximum temperature this day

Note that the first four functions above (which should be very simple) will not actually look at the data frame that they are given as their first argument.

You should also write a function called predictions, which makes forecasts for all days from some start index to the last day for which data is provided. This function will take as its arguments a function to use for forecasting, a data frame with values that may be used for forecasting, the series of values for which forecasts are to be made, and the start point for making forecasts of this series. It should return a vector of forecasts for values in the series from the specified start point to the end.

These functions (plus the predictions2 function described below) should be defined in a script file that does nothing except define these functions, since these functions are of general use, perhaps for other data sets than the one we are using for this assignment.

In another script file, you should read in the Houston data (with the command shown above), and make forecasts for the numbers of deaths and maximum temperatures in Houston. You should start your forecasts at the beginning of the third year of the data provided, so that all forecasts will have at least two years of data available (though not all the forecasting functions above will use all this data). In other words, you should set the "start" argument for predictions to 365 + 365 + 1 (since 1994 and 1995 are not leap years); this should result in your making predictions for 1827 days.

You should make predictions for the numbers of deaths with each of the seven forecasting functions described above, and for maximum temperatures with the first six. For each of these 13 forecasts, you should produce (and hand in) a set of four plots - put together in one big plot, by using par(mfrow=c(2,2)) - which show the actual values versus time index, the forecasts of these values vesus time index, the errors in these forecasts versus time index, and the errors in these forecasts versus day of the year. These plots should have appropriate titles that identify what they show. You may find it convenient to write a function that calls predictions and makes these plots, which should be defined in this script file, not the one with general funtions definitions, since it is specific to this assignment.

You should also output the average absolute value of the error for each of these forecasts.

Finally, you should try a more elaborate forecasting method, in which you first make forecasts with some method, and then try to use another method to forecast the error in the first method. The idea is that if you can manage to forecast the error well, you can get a better forecast by just adding the forecasted error to the original prediction.

You should write a predictions2 function that does this. It should take two forecasting functions are arguments, along with a data frame of variable to use, a series of values to forecast, and two starting points, the second of which is later than the first. It should use the first forecasting function and the first starting point to make a first set of forecasts, then find the errors in these forecasts, and try to forecast these errors with the second forecasting function, starting at the second starting point. It should return forecasts (starting at the second starting point) that are equal to the first forecasts plus the forecast error in these forecasts.

You should try out predictions2 for predicting deaths, with the first forecasting function being forecast_same_month and the second being forecast_similar_tmax, and produce plots and output as for the other forecasts.

Your email should have the following attachments:

The R script file containing your function definitions, called a2funs.r
The R script file that makes predictions for the houston data set, as described in the handout, called a2script.r
PDF files produced by the script, containing the set of four plots that the handout says to produce for each forecasting method and each quantity (deaths or tmax). You can attach 14 PDF files, or it's OK to attach one PDF file that has all the plots (created with pdf("a2plots.pdf"); stuff to make all plots; dev.off()). The text output of this script, which should give the average of the absolute value of the error for each of the forecasting methods. You may omit this if you show this information in the titles of the plots.

You can create a text file with the text output by creating a text file with File > New File > Text File in RStudio, then copying and pasting into it.

You can create a file with a plot using the Export menu in the RStudio plot window (choose "as PDF"). You can move back and forth between multiple plots using the left and right arrows in the plot window.

Reference no: EM131319127

Questions Cloud

Expected mean lifes for the population : Determine 1× (once) and 2× (twice) the expected mean lifes for the population, as well as their corresponding cumulative-probability function values.
What is present value-yield to maturity of this annuity : Myers, Inc. will be making annual loan payments of $3,895.50 for a 10-year period starting at the end of this year. If the interest rate is 9 percent, what is the present value of this annuity? Jenny Clayton is looking to invest in some 5-year bonds ..
How will the capability be relied upon during a crisis : What vital information, capability, or capacity does the city need from this entity, and why? What mutual support can the city provide the entity in return?
What issue concerning ethical dilemma of illegal immigration : What would you present (if only given 2-3 minutes to speak or 250-300 words to write) as the primary issues concerning the ethical dilemma of illegal immigration, specifically whether the United States government should allow undocumented immigran..
Write R functions for forecasting future values : Write R functions for forecasting future values of a time series, and apply them to observations on numbers of deaths and maximum temperatures in Houston, Texas. Doing this will provide more practice in basic R programming, and on the use of data ..
Relationship between ethical theory and moral practice : Discuss the crucial relationship between ethical theory and moral practice, considering the claim that good ideas are "valuable because they allow us to do thing well. Any good theory will be a useful tool in the laboratory of life" (Wilkens, 2011..
Describe fully elco powers and the communitys ability : For each threat, describe the types of crises that might result and how these would likely impact the city of Solace. For each threat, describe fully Elco Power's and the community's ability to do the Prevent the threat or likely crisis.
What is the firm cash conversion cycle : Bain & Company has the following data. What is the firm's cash conversion cycle?
How the effectiveness of skilling''s method scales : [Hint: Skilling's method is invariant under affine transformations, so the rejection probability at equilibrium can be found by looking at the case of a separable Gaussian.]

Reviews

Write a Review

Programming Languages Questions & Answers

  Determining whether it is possible to reuse code

You are a project manager and have been told that you need to reuse some given code in your new development effort. How would you go about determining whether it is possible to reuse code for this effort?

  Using a loop to perform operations multiple times

What I need help with is to get the dice to roll 100 times instead of just one. So the read file results will show the results of 100 rolls of the dice

  Write a program with a windows-style interface

write a program with a Windows-style interface to carry out the task. A form contains two text boxes and one large label between them with no preset caption. When the first text box receives the focus, the label reads "Enter your full name.

  Linear programming staffing problem

South Central Utilities has just announced the August 1 opening its second nuclear generator at its Baton Rou, Louisiana, nuclear power plant.

  Design application that plays a guessing game with numbers

Design and implement an application that plays a guessing game with numbers. The program should generate a random number between 1 and 100 (inclusive), then repeatedly prompt the user to guess the number

  Design a small dictionary using the binary search tree

Design a small dictionary using the Binary Search Tree data structure. Each entry may include a word and its definitions. You should create the tree structure/dictionary using data input from a text file. After the dictionary is created, the user ..

  Implementing class called card for standard playing card

Design and implement a aclass called Card that represents a standard playing card. each card has a suit and a face value.

  How many credits they have taken in network systems

Write a program that will take in basic information from students, including their names and how many credits they have taken in Network Systems Administration program

  Develop console application to generate random number

Develop C++ console application which generates the random number which should be guessed by user. Program will indicate to user if their guess is correct, too high or too low.

  Write a shell script to read from the keyboard

Write a shell script called poject. 21 to read from the keyboard and display on the monitor first name, last name and age if the age is less than 50. If the age is greater than 50, it will display your are over 5 instead of the age.

  Calculate the average rainfall for three months

Write a program that asks the user to enter five floating-point numbers. The program should create a file and save all five numbers to the file.

  Design program to compute-display number of miles per hour

Design a program that calculates and displays the number of miles per hour over the speed limit that a speeding driver was doing.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd