American University R programming Exercise Paper This assignment contains concepts and questions to help you review topics from the first 4 lectures. Howev

American University R programming Exercise Paper This assignment contains concepts and questions to help you review topics from the first 4 lectures.
However, the concepts listed below may not be comprehensive. Learning objectives:– Review R basics and R Markdown – Review functions, Piping, Conditionals. – Review graphics (ggplot2) – Practice and review data frame manipulation (dplyr) HW 4. Data Frame and Review of Topics 1 – 4
Stat 412/612 Statistical Programming in R
Due: Tuesday, June 2
Instructions
• This assignment contains concepts and questions to help you review topics from the first 4 lectures.
However, the concepts listed below may not be comprehensive.
• Learning objectives:
–
–
–
–
Review R basics and R Markdown
Review functions, Piping, Conditionals.
Review graphics (ggplot2)
Practice and review data frame manipulation (dplyr)
• Covered Chapters: 1, 2, 3, 4, 5, 17, 18, 19, 27 of RDS.
• Total points: 18. (Some problems will be graded in details. Others upon completion.)
Concepts
• R Basics
– Data types (numerics, characters, logicals, vectors, data frames), assignment, basic arithmetic,
calling functions (and reading the function help files), indexing vectors, logical operators, extracting
vectors from data frames.
• R Markdown
– Formatting and knitting R Markdown files. Should be able to have a mix of code and plain text.
• Functions, Piping, Conditionals
– Using pipes to chain operations, creating functions, using if/else if/else statements to conditionally run code.
• Graphics
– The ggplot2 system of graphics, point/line/histogram/smooth/density/rug/boxplot geoms,
color/size/shape/transparency aesthetics, faceting, themes, positions, scales, labels, saving plots
• Data Frames
– dplyr system of data frame manipulation, select(), filter() (using logical operators), mutate(),
arrange(), summarize(), group_by().
Exercise 1: Pygmalion Data
• For this exercise, we will use the Pygmalion data from Ramsey and Schafer’s “Statistical Sleuth”, 3rd
edition. For a brief description:
1
– Pygmalion Effect: high expectations of a supervisor translate into improved performance of
subordinate.
– A company of soldiers contains three platoons each.
– Within each company, one platoon was randomly selected to be the “Pygmalion platoon.”
– The platoon leader in the Pygmalion platoon was told by the army psychologist that his platoon
was predicted to be superior.
– At end of basic training, all soldiers in each platoon were given a skill test.
– Data consist of average scores for each platoon.
• You can load these data into R after you the Sleuth3 R package)
# install.packages(“Sleuth3”)
library(Sleuth3)
data(“case1302”)
1. (1 pt) What are the observational units? What are the variables?
2. (2 pts) Calculate the average score within each company by treatment combination. (Hint: we’ll need 2
variables in group_by() function for this problem.)
3. (2 pts) Create the following plot:
90
Treatment
Mean Score
85
Control
Pygmalion
80
Number of
Observations
75
1
70
2
65
C1 C10 C2
C3
C4
C5
C6
C7
C8
C9
Company
4. (1 pt) Does it make sense to add a loess smoother to the above plot? Why or why not? If so, add one.
Exercise 2: Midwest Data
• For this exercise, we’ll use the midwest data from ggplot2.
1. (1 pt) Load these data into R. What are the observational units?
2. (1 pts) Calculate the total population in each state. Display the data in an appropriate plot. (Hint:
check geom_col().)
3. (2 pts) Make a scatterplot of population density against the percent college educated on a scale that is
appropriately linear. You should exclude counties in the bottom tenth percentile of total population.
2
Color code the counties by state. Add the overall OLS line (the one that takes all points into account).
Make the OLS line black and dotted.
4. (2 pts) Make an appropriate plot to explore the association between the state and the percentage adults.
Make sure the axis labels are nice and use the black-and-white theme.
5. (1 pts) Find an R function to determine all possible values of state in the data set.
6. (1 pt) In the state variable, replace IL with Illinois. Replace IN with Indiana, etc. . . (Hint: try
functions recode() or replace().)
Exercise 3: Coding Practice
1. (1 pts) Create a function that takes a vector of numerics as input. It checks the length of the vector. If
the length is even, it returns the sum of the vector. If the length is odd, it returns the sum of all of the
even numbers of the vector. For example, the following are some outputs of my implementation, called
lsumfun().
lsumfun(c(1, 2, 3, NA))
## [1] 6
lsumfun(c(1, 2, 3))
## [1] 2
lsumfun(c(2, 3, 4, 5, 6))
## [1] 12
lsumfun(c(2, 3, 4, 5, NA))
## [1] 6
lsumfun(c(2, 3, 4, NA, 6))
## [1] 12
2. (2 pts) We add a Leap Day on February 29, almost every four years. In the Gregorian calendar three
criteria must be taken into account to identify leap years:
• The year can be evenly divided by 4, is a leap year, unless:
• The year can be evenly divided by 100, it is NOT a leap year, unless:
• The year is also evenly divisible by 400. Then it is a leap year.
This means that in the Gregorian calendar, the years 2000 and 2400 are leap years, while 1800, 1900,
2100, 2200, 2300 and 2500 are NOT leap years
Write a function that takes the year as input and returns TRUE if it is a leap year and FALSE if it is not
a leap year. Evaluate your function at 2, 12, 200, 800.
3. (1 pts) Create a function that takes two vectors of numerics as input, a and b.
•
•
•
•
•
•
Your function should terminate with an error message if either a or b contain repeated numbers.
If a number is in a and not b, you add 1 to your score (initial score is 0).
If a number is in b and not a, you subtract 1 from your score.
If a number is in both a and b, you do nothing to your score.
The function returns the final score.
Hint: help(“%in%”)
In my implementation, score2(), the following are some evaluations.
3
score2(c(1, 2, 3), c(3, 4))
## [1] 1
score2(c(1, 2, 3), c(1, 2, 3, 4))
## [1] -1
score2(c(1, 2, 3), c(3, 4, 4))
## Error in score2(c(1, 2, 3), c(3, 4, 4)): b has some repeated numbers
Explanation for the score2(c(1, 2, 3), c(3, 4)) example:
•
•
•
•
•
•
Vector a is (1, 2, 3), vector b is (3, 4).
Vector a does not have repeated elements. Vector b does not have repeated numbers.
Numbers {1, 2} are in a, but not in b. Score + 2
Number {4} is in b, but not in a. Score -1
Number {3} is in both a and b. Score no change.
Final score is: +2-1 = 1
4

Purchase answer to see full
attachment

Don't use plagiarized sources. Get Your Custom Essay on
American University R programming Exercise Paper This assignment contains concepts and questions to help you review topics from the first 4 lectures. Howev
For $10/Page 0nly
Order Essay
admin

Recent Posts

Economic Debate #3- Progressive Income Tax – The Homework Helper

Economic Debate- Progressive Income Tax For this Economic Debate, we are going to discuss the…

2 years ago

MKT 6120 – Marketing Management – Davis Learning Engagement #7

TOPIC: Going Global Discussion Thread 1 (initial post due Wednesday for full credit) Please note:…

4 years ago

jvjvjhvjhvhjvj

Assignment Topic This week will culminate in the creation of a narrated PowerPoint to create…

4 years ago

Students are supposed to select a technological organization of their choice.

The Assignment must be submitted on Blackboard (WORD format only) via allocated folder. Assignments submitted…

4 years ago

Increases the risk of wildfires

you need to post your 2-page information flier to share with your Final Project Group.…

4 years ago

Statistics for Technology management

discussion: Discuss the methods used at your company to measure and ensure quality products and…

4 years ago