Home Coursework Help WhatsApp Us About us

STAT 170 – Elementary Statistics Quantitative Reasoning Project
Use the following steps to test hypotheses about your statistics project.

When the problem involves hypothesis testing, use the following structure for written reports.
• Step 1: State the hypotheses.
• Step 2: Summarize the data for your readers.
• Step 3: Give the value of the test statistic and the p-value.
• Step 4: Use the p-value to draw a conclusion. State the conclusion in statistical
terms: Reject Ho in favor of Ha, or retain Ho (fail to reject Ho).
• Step 5: State the conclusion in layman terms and in context of the application. Use the
p-value to state the strength of the evidence.
• p-value > .10
retain Ho – there is insufficient evidence to reject Ho in favor of Ha
• .05 < p-value ≤ .10
gray area -- decision to reject Ho or retain Ho is up to the investigators – there is some
evidence against Ho and in support of Ha
• .01 < p-value ≤ .05
reject Ho in favor of Ha – there is fairly strong evidence against Ho and in favor of Ha
• .001 < p-value ≤ .01
reject Ho in favor of Ha – there is strong evidence against Ho and in favor of Ha
• p-value ≤ .001
reject Ho in favor of Ha – there is very strong evidence against Ho and in favor of Ha

Use your TI-83/TI-84 calculator for all of these problems. You will not need any tables.
Use the Sample Test 3 Questions—Answer Key (posted in Canvas) as an example of what my
expectations are.

Working With R Files

Save your work to a .r file. Name yor file like _<.First Name>_HW4.
Upload your files to DropBox.

I have provided you with a .csv file called Insight.csv. It contains the log of my Honda Insight’s mileage since I bought it. There is a header row at the top of the spreadsheet. The columns are mostly self-explanatory, but just in case:

Date: the date of the fill up
Miles: the number of miles on the odometer when I filled up
Gallons: the number of gallons I put in the car
Price.per.Gallon: the price per gallon of gas
Total.cost: the total cost of the fillup (i.e., Price.Per.Gallon * Gallons)
Grade: the grade of gasoline purchased
MPG: the calculated miles per gallon for that tank (Miles/Gallons)
Price.Per.Mile: Total.cost/Miles
Cumulative.Miles: cumulative miles driven by the car
Cumulative.Gallons: cumulative gallons of gas purchased
Cumulative.Cost: cumulative cost of gas purchased
Cumulative.MPG: Cumulatove.Miles/Cumulative.Gallons
Cumulative.Price.per.Mile: Cumulative.Cost/Cumulative.Miles
Gas.Source: the chain of gas station the gas came from
Car.Said: the mileage for that tank that the car’s on-board computer reported
Delta: the difference between the mileage the car said and the mileage I calculated
Average.Price.of.Gas: cumulative average price paid per gallon of gas
Avg.Temp: average temperature for the period since the last fill-up, taken from a website at UD

Given this data, write an R script that will produce a SEPARATE WINDOW with this output.

r statistics output.png

Note that the bottom right chart may not display all the labels on the X-axis until the window is maximized….that’s okay.

IMPORTANT NOTE: There are several gas stations that only have one or a small number of entries. Those should be INCLUDED in the data for the first two charts, but EXCLUDED when producing the bottom two charts. The following code will quickly accomplish this for you:

Assuming you have read the data into a dataframe called mileage:

tbl <- table(mileage$Gas.Source)
new.Mileage <- droplevels(mileage[mileage$Gas.Source %in% names(tbl)[tbl>10],,drop=FALSE])

The dataframe new.Mileage now has the data minus any entries where there were fewer than 10 observations at that Gas.Source.

Your script should begin with reading in the data. You can assume that your script and the data file are both in the working directory, so you need to only reference the filename (Insight.csv) and not worry about the file path.

  1. Write an R function called Cleaner that accepts a single vector of numbers that may contain NA entries and returns a vector where the NA’s have been replaced with -1.
  2. Write an R function that accepts three parameters: a lower bound, an upper bound, and an increment. Then use a repeat loop to generate a vector of the numbers from the lower bound to the upper bound by increment.
    For example, if my function was called counter
    answer <- counter(2,10,2)
    [1] 2 4 6 8 10
    i.e. the numbers from 2 to 10 in increments of 2
    answer <- counter(2,10,3)
    [1] 2 5 8
  3. Assuming I have three variables called lower, upper, and increment, how could I produce the same thing as number 2 with a single R statement that does not employ a loop?
  4. Write an R function that accepts two parameters: a vector of strings and a single search character. The function will then return a vector that contains the input strings that contain the search character.

For Example, if my function was called searcher
names <- c(“Bob”, “Bill”, “William”,”Tom”)
answer <- searcher(names,”i”)
[1] “Bill” “William”

  1. Write an R function that accepts three parameters: a vector of strings, a single search character, and a single replacement character. The function will return the vector of strings, but with all instances of the search character replaced with the replacement character.
    For example, if my function was called replacer
    names <- c(“Bob”, “Bill”, “William”,”Tom”)
    answer <- replacer(names, “o”, “O”)
    [1] “BOb” “Bill” “William” “TOm"

    # Question 5

    byday <- table(FM$Date)


Average Number of Songs ListenedTo Over the Time Priod

avsongs <- mean(byday)

Maximum Songs Listened To per Day


I have provided you with an Excel spreadsheet called Last_FM_data_shuffled.xlsx. It contains the log of all the music I have listened to on my phone since I began using the Last.fm website. As the name implies however, I have shuffled the entries so that they are no longer in chronological order. There is a header row at the top of the spreadsheet, and there are four columns of data: Band, Album, Song, and Date.

  1. Assuming you are not using packages that let you read from Excel, what must you do first in order to prepare this data to import to an R dataframe? What command will you use to import it?
    For this problem, submit a .r file where the first line is a comment telling me what you have to do, and the second line is the R command to import the data. Remember that # is the comment character.
  2. What is a single R command that can be used to count how many different bands are represented in the data file?
  3. Write an R script that will sort the data back into chronological order and store it in a new dataframe.
  4. Recall that the table() function can be used to quickly summarize data. As an example, assuming I have attached the dataframe with the song data, I can type


And get the following output

(Song For My) Sugar Spun Sister 1901 45

                          2                        1               2

     50 Ways to Say Goodbye     6th Avenue Heartache      8:02:00 PM 
                          1                        2               1 

Each song title appears as a column heading and the number underneath it represents the number of time the song appears in the Song column of the dataframe.
Using this, what is the R command to determine the name of the song that has been played the most times? What is the R command to determine how many times that song has been played?

  1. Using R, determine the average number of songs I listened to per day over the time period in the dataset.

General Instructions

15 questions. The first 12 are fill-in-the-blank/short-answer type questions, while the final three require you to manipulate a dataset. You should have 4 Total files

For problems 13-15, assume that your script and the data file are both located in the current working directory. Therefore, when you read a file in, there is no need to provide a path, just the filename. For example:
myData <- read.csv(“inputFile.csv”, header=TRUE) CORRECT
myDaya <- read.csv(“C:\WSUDocs\Desktop\inputFile.csv”, header=TRUE) WRONG

When you are asked to write a function, all the information that function needs to operate should be included in the parameters. Thus, there should be no user input involved in the function itself.
For questions which ask you to produce a plot, you may use either base R plotting or ggplot2, whichever you prefer. Your plots should have appropriately labeled axes.

  1. Given a vector x:
    x <- c(4,6,5,7,10,9,4,15)

What R command could I use to find out how many entries in X are less than 7?

  1. Complete the following R command to generate a vector of the integers from 1:10

x <- 1______

  1. What is the default separator character in the paste() function?
  2. A categorical variable with a fixed number of levels is a _________________.
  3. _ changes the class of a variable, if possible.
  4. What function do you use in R to add a column to a matrix or data frame?
  5. Given the vector y:
    y <- c(101,85,97,102,76,89,95,94,90,80,82,75,103,100,79,69)

What R command could I use to replace any value greater than 100 with 100?

  1. Write a short R function called getNames that accepts two parameters called namesVector and excludeCharacter and returns all the entries in namesVector that DO NOT contain excludeCharacter.
  2. Would the following data be considered WIDE or LONG?
    Control Treatment Preheated Treatment Prechilled Treatment
    6.1 6.3 7.1
    5.9 6.2 8.2
    5.8 5.8 7.3
    5.4 6.3 6.9
  3. Given a matrix x, what R command would I type to return all the columns in row 5?
  4. Given two vectors:
    x <- c(3,2,4)
    y <- c(1,2)

The command z <- x*y will produce a vector z that contains (3,4,4). Explain why this happens.

  1. Write a short R function called replaceMean that accepts a parameter called numberData and returns a vector where any missing data has been replaced with the mean of data. For example
    x <- c(1.2, 7.9, 3.4, NA, 4.2, 9.1, NA)
    z <- replaceMean(x)
    z would now contain (1.2, 7.9, 3.4, 5.16, 4.2, 9.1, 5.16)
  2. I have provided you with a dataset in the file MetroMedian.csv. The file contains the median price per square foot of housing in each of the nation’s largest 557 metropolitan areas from April 1996 through December 2016. There are a number of missing entries where the data was not available.
    a. Read this file into a dataframe called metro
    b. This data is not correctly formatted. Produce a dataframe called tidyMetro that is suitable for analysis.
    c. For the entire data set, what is the mean value for the STATE of New York? Show both the R command(s) and the result
    d. Write an R function that accepts two parameters called valueFrame and searchRegion and returns the mean value for all entries for that region.
  3. I have provided you with a dataset in the file BeachWaterQualiy.csv. The file contains the bacterial count results from New York beaches from May 2005 to May 2016.
    a. Read this file into a dataframe called beaches
    b. When there was no detectable level of bacteria, the Results field was left blank. These fields were read into the dataframe as NA. Write a single line of R to replace any NA values in the Results column with 0 (zero).
    c. The sample dates recorded in this dataset are not suitable for ordering the data chronologically. Add a column to the beaches dataframe called new.date that is suitable for sorting.
    d. Write an R function called beachPlot that accepts three parameters called beachData, beachName, and sampleLocation. Your function should produce a line plot of the sample results for the named beach and sample location. For example, if I were to call
    beachPlot(beaches, “MANHATTAN BEACH”, “Center”)
    the function would return a plot that looked similar to

You may assume the dataframe being passed into the function has a column with a sortable date, but you cannot assume that the dataframe is chronologically sorted.

  1. I have provided you with a dataset in the file insight.csv. This is the mileage data (again) for my Honda Insight.
    a. Read this file into a dataframe called mileage
    b. Produce and label a scatterplot with blue points that puts Average Temperature on the x-axis and MPG on the y-axis
    c. Add a blue line that fits a linear model
    d. Add red points the put Average Temperature on the x-axis and the “car.said” mileage on the y-axis
    e. Add a red line that fits a linear model
    f. Add a legend that tells me which points are “Measured MPG” and which points are “Car Reported MPG”. You can specify the location of the legend or let the user select it, whichever you wish.

A datafile containing all the values has been attached here with;-