Posts tagged with r statistics help

R Statistics Help

Question 13.
a.

read.csv()
metro <- read.csv("MetroMedian.csv", header = T)
reads the file from the folder and store to the dataframe “metro” with header=T so that it can retain variable names from the file.
b.
install.packages("reshape2")
install.packages("data.table")

The package “Reshape” is installed so that its function ”melt()” can be used to create molten data from the matrix read. The “data.table” package is loaded so it can transform the dataframe for easier manipulation. Load the data.table after the “reshape” package.

tidyMetro <- melt(metro,id.vars=c("RegionName","State","SizeRank"),variable.name="date",na.rm=TRUE)

use the melt function from the data.table library, to convert the dataframe from Wide to Long. The function has the “object” to be converted, the factors and the variable of interest as the inputs, the na.rm=TRUE, drops the empty cells.
c.

mean(tidyMetro$value[tidyMetro$State=="NY"])
use the r function mean(), That’s is select value from the tidyMetro dataframe, where state==”NY”
d.
regionMean <- function(valueFrame,searchRegion) {
mean(valueFrame$value[valueFrame$RegionName==searchRegion])

}

The function above is stored in the variable region. The inputs of the variable are;- the object and the searchRegion. Inside the function, we use the r-function mean(), which selects values from the variable of interest where the state name is same as the searchRegion entered to the function.
Question 14
a.

beaches <- read.csv("BeachWaterQuality.csv", header = T)
because the data is stored in an excel .csv format, use the function read.csv(), to read the excel file using the columns names as the variable names.
b.
beaches$Results[is.na(beaches$Results)] <- 0
select the variable Results from the beaches dataframe and check if it is NA, assign 0 to the empty value.
head(beaches)
check the format of the dataframe.
c.
new.Date <- strptime(as.character(beaches$Sample.Date),"%m/%d/%Y")\
create a variable new.data which is in r-local format, using the r–function strptime(), as.character converts the date variable to be of character type so that its format is understood. The “m/%d/%Y”, tells are the date format from the file is month/date/ and Year written in four digits. The month and date does not contain the leading 0.

beaches$new.date <- new.Date
Add the newly created variable;- new.Date to the beaches dataframe and assign the name new.Date.
d.
beachPlot <- function(beachData,beachName,sampleLocation){
beaches2 <- subset(beachData, Beach.Name==beachName & Sample.Location==sampleLocation)
plot(beaches2$new.date,beaches2$Results, ylab = "Bacterial Count", xlab = "Date",main=c('Bacterial count for', beachName,sampleLocation))
lines(beaches2$new.date[order(beaches2$new.date)], beaches2$Results[order(beaches2$new.date)],
xlim=range(beaches2$new.date), ylim=range(beaches2$Results),col="red")
}

Create a function and assign the name beachplot. The inputs to the function are;- the dataframe, beachName and sampleLocation. Use the function inputs to subset the dataframe and store the subset to the beache2 dataframe. The subset dataframe is selected from the input dataframe. The rows that have beachName and sample location are selected. Use the plot function to create a plot by entering the x-axis variable, y-axis variable, the y-axis label the x-axis label and main title label which is entered as a vector so that it can get the function input factors.
Add lines to the plot for Results against Date. The line uses the data range and the plot has a red color.

Question 15.
a.

mileage <- read.csv("Insight (3).csv", header = T)
head(mileage)
The data set in the directory is stored in excel .csv format with the name Insight. So use read.csv() function to read the data file and store the variables in a dataframe called mileage.
Check the structure of the dataframe using the head(), function.
b.

plot(MPG~Avg.Temp, data = mileage, ylab="",xlab="Average Temperature",main="MPG against Avg.Temp and Car Said",col="blue")

plot function to create the plot. The tilde sign means y~x. And get the values for x and y axis from the mileage dataframe. Leave the y-axis empty because another line will be added after. Label the x-axis because both variables are being plotted against the same x-varibale. Add a title using main=”” and set the colour for this plot to blue.
c.

abline(lm(MPG~Avg.Temp,data = mileage),col="blue")
Add a trend line to the plot. The line to be added is the line of best fit from a linear model formed using the dependent and the response variable, Get the variables from the mileage dataframe. Set the color of the trendline to blue using col=”blue” command.

D ~

par(new = TRUE)
par() is an r-function used to combine plots. So setting new=T, allows a new plot to be embedded in an existing plot.
plot(Car.Said~Avg.Temp, data=mileage,col="red",ylab="MPG/Car Said",xlab="",axes=FALSE)
use the plot() function to add a new line to the existing plot. Set the color to red and add the y-axis label. Axes=FALSE suppresses the axis values.

e Adding a Red Line, that Fits the Linear Model

abline(lm(Car.Said~Avg.Temp,data = mileage),col="red")
create a linear model and Add a trendline for the 2nd plot. Set the colour to red.
par(new=FALSE)

legend("topleft",legend=c("Measured MPG","Car Reported MPG"),

   text.col=c("blue","red"),pch=c(16,16),col=c("blue","red"))

Add a legend to the plot. Place the legend to the top left of the plot. The labels of the legend should be “Measured MPG” and “Reported MPG”, the colors of the text are red and blue, pch-sets the width of the line and color them with “blue” and “red respectively.

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.

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

head(table(Song))

And get the following output

Song
(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;-
R.docx