Create histograms of the PTRatio in the Oklahoma schools. Make sure the layout, labels, and title all match the sample output.

For the scripts created below. The entire console needs to be displayed. So the graphics and the script can be seen.


  1. At the top of your script, include housekeeping steps to first list the contents of the workspace

and then clear it out.


  1. Load the HW04 workspace that you downloaded from eCampus for the previous assignments.

You may use the file menu on the R console to browse for and load the workspace. When this is

completed, copy the line of code from the console window and paste it into your script so you

can load the workspace automatically the next time you want to run the script. After loading

the workspace, include a step that lists in the console the contents of the workspace again to

confirm the desired data file has been loaded.


  1. Include a statement that will cause R to send all of the graphics in this assignment directly to the

PDF file that you will be posting on WebAssign. NOTE: You may want to wait until you have all

of your graphics working properly before including this step so you can see the graphics in the

console while you are working on them.


  1. Create histograms of the PTRatio in the Oklahoma schools. Make sure the layout, labels, and

title all match the sample output.

  1. Create the first histogram using the default number of breaks.
  2. Create a vector that can be used to extend the length of the X axis and force breaks

every 5 pupils/teacher. You may find it helpful to use the statistics on the PTRatio

column to determine how far your sequence should go.

  1. Create a new histogram with break points at 5 pupils/teacher.


  1. Add a maroon line to the graph that shows the normal distribution density of the PTRatio

numbers. Feel free to use separate lines of code as necessary to create the vectors required for

this step.


  1. Draw a yellow vertical line on the previous histogram at the average PTRatio. Use a function to

determine the position of the line instead of hard coding in the mean value. Use a value from

the color palette to specify the color instead of using the color name.


  1. Create a new plot showing the number of teachers compared to the Pupil Teacher Ratio

(PTRatio). Use the plot character shown in the sample output on eCampus. The color of the

plot character is purple. Force the x axis to extend to 140.


  1. Add a colored fit line to the plot. Use a hex value to mix a color for the line that is FF parts Red,

99 parts Green and no Blue. In the console, show the summary statistics for the model that

created the fit line.


  1. Imbed the date and time of creation near the upper right hand corner of the graph area. The

coordinates of the time stamp location need only be close to the ones used for the sample

output. This time stamp must be based on system time so that it updates whenever your code

is run.


  1. Create a boxplot of the number of students in each grade (Columns Grade7 through Grade12).

Make sure the labels, etc. match the posted output. The inside of the boxes is light green.



  1. Add diamonds that can be colored and filled that represent the average number of students in

each grade from Tulsa County. You may create the vector of averages first to simplify the

function that is used to plot the diamonds. The background of the diamonds is dark green and

the border is red. Make the size of the diamonds larger than the default similar to what is

shown in the sample output. (TIP: Do not over-complicate the plotting of the diamonds. You

can plot a vector without supplying the x component of the points if the vector labels match the

data used to create the original graph as it does in this case.) You may find it helpful to consult

the R help on points in order to find out which plot character to use and how to specify the

background color.




Then On another Script complete the following


Create a function. The creation of a function is an iterative process. The first

step of that process (explained in #3 below) is to get the code working as standard R code with all of the

values hard coded. After the process has been successfully tested with the hard coded values, convert

the process to a function and replace the desired hard coded values with function parameters. You may

want to delay sending output to PDF until you have your manual process working as desired.


An exponentially weighted moving average (EMA) is frequently used in the finance industry to form a

stock price trend indicator while smoothing price action and filtering out noise. EMA for a given time

period is calculated using the following formula:



P is the closing price at the end of that time period and N is the number of periods to include in the

moving average. For those who may not know, the “a” looking character is the Greek letter alpha.


Along with the header, your R script will include two major sections. The first section will graph the EMA

of stock prices (adjusted closing price) using hard coded values. The second section will contain the

code to create the function, the appropriate graphics setup, and two calls to the function. The console

and PDF output should represent only the final submission of your R script after restarting R.

  1. The file cisco.csv contains stock price information for every day since Cisco stock started trading

publicly. Download the file from eCampus and become familiar with its contents. Write a line

of code that reads this file into a data frame in your R session for use in this assignment.


  1. When you define your PDF document and open the device, set the size to 11 X 8.5 inches to

match the size of a normal letter size sheet of paper turned so that it is wider than tall.

(Research the help documentation on pdf to determine the correct options to use.)


  1. Create and plot a 30 Day Exponential Moving Average with these steps:
  2. Go ahead and assign a value for the alpha component of the formula so the EMA code

does not get too long later on. If you wish you may also assign a scalar the value of 30

for our N value and use it in your code since this will be an argument to the function

later in the assignment.

  1. Create a vector populated with zeroes that will be used to contain the EMA values. The

length of this vector needs to be the same as the length of the vector of adjusted closing

prices from the Cisco data.

  1. One of the problems with moving averages is that they rely on a previous value and, at

some point in the beginning, there is no previous value. We are going to solve this

problem in this instance by populating the EMA on day 30 with the simple average

adjusted closing prices of the first 30 days in which Cisco stock was traded. Create an

assignment statement that puts the 30 day average in the 30th member of the EMA


  1. Use a loop to run the EMA formula repetitively from day 31 to the last day for which

Cisco stock prices are available. Each iteration of the loop will use the corresponding

day’s adjusted closing price to calculate the EMA value for that day and store the

calculated value in the appropriate place in the EMA vector.

  1. There are roughly 260 weekdays in a year. Plot a line showing the last 260 EMA values.

You may simply use the numbers 1 through 260 as the x values and then use the EMA

values for y. This line should be purple. The range of the Y axis is to be 0 to 35. To

make our program fully adaptive, we would need to use an algorithm to base the upper

limit on the maximum stock price but this assignment is going to take a long time

without adding this requirement so hard code the values of 0 and 35 for now. We will

provide an argument to adjust this in the function. Set the title and axis labels as


  1. At the bottom of the graph, add the EMA formula as shown. Use the point 0,2 as the

reference point for the formula line.

  1. Add to the graph a green2 line showing the actual adjusted closing prices for the last

260 days available in the Cisco data.


  1. Convert the completed code to a function including the code that will plot the graph. The first

argument to the function is to be the name of the vector containing the adjusted closing prices

so that we can analyze data from any stock without having to rewrite our function. The second

argument will be the number of days over which the EMA is computed. The default value will

be 30 like we used above. Be sure to replace all references to the price vector and the number

of days with the appropriate function argument. Note that any value in your original code that

was based on the 30 day average will use the N argument or an algebraic expression based on

  1. The third argument will set the upper limit for the Y axis. Make the default 35 and make sure

the graphing expressions use this argument. The graph title and formula line must be

constructed so they automatically change according to the value of N used in the function call.


  1. Set up your graphics for this section of the script so that there will be two columns of graphics

on the output page. We want to set the overall margins of the graphics within the page to be

1.5 inches on the top to allow space for punched holes and 0.5 inches on the bottom, right, and

left. Set the margins of the individual graph to be 2 lines on top, 4 lines left, 0 lines right and 4

lines bottom.


  1. Call your function twice. The first call should have the reference to the adjusted closing price

column as its only argument. For the second call, add a value of 100 for the N argument. (When

you are testing your program, you should experiment with an N of 10 and a few other values to

see how the size of the EMA affects the graph. But, do not include the results of these tests in

your homework submission.)


  1. Write the system time in the bottom outer margin of the page. Left justify the time as shown in

the sample.


  1. After you have finished writing and testing your code close R. Then reopen R and run your

script. Submit to WebAssign the script, console, and output from this final run.

Type of paper Academic level Subject area
Number of pages Paper urgency Cost per page: