Question: Please solve the edit me parts using R coding ##### Homework tips: 1. Recall the following useful RStudio hotkeys. Keystroke | Description ----------|------------- ` `

Please solve the edit me parts using R coding

##### Homework tips:

1. Recall the following useful RStudio hotkeys.

Keystroke | Description ----------|------------- `` | Autocompletes commands and filenames, and lists arguments for functions. `` | Cycles through previous commands in the console prompt `` | Lists history of previous commands matching an unfinished one `` | Runs current line from source window to Console. Good for trying things out ideas from a source file. `` | Aborts an unfinished command and get out of the + prompt

**Note**: Shown above are the Windows/Linux keys. For Mac OS X, the `` key should be substituted with the `` (⌘) key.

2. Instead of sending code line-by-line with ``, you can send entire code chunks, and even run all of the code chunks in your .Rmd file. Look under the menu of the Source panel.

3. Run your code in the Console and Knit HTML frequently to check for errors.

4. You may find it easier to solve a problem by interacting only with the Console at first, or by creating a separate `.R` source file that contains only R code and no Markdown.

### Problem 1: Tables

This problem uses the birthwt dataset, which we load and manipulate in the code chunk below.

```{r} library(tidyverse) library(knitr) birthwt <- as_tibble(MASS::birthwt)

# Rename variables birthwt <- birthwt %>% rename(birthwt.below.2500 = low, mother.age = age, mother.weight = lwt, mother.smokes = smoke, previous.prem.labor = ptl, hypertension = ht, uterine.irr = ui, physician.visits = ftv, birthwt.grams = bwt)

# Change factor level names birthwt <- birthwt %>% mutate(race = recode_factor(race, `1` = "white", `2` = "black", `3` = "other")) %>% mutate_at(c("mother.smokes", "hypertension", "uterine.irr", "birthwt.below.2500"), ~ recode_factor(.x, `0` = "no", `1` = "yes"))

```

##### (a) build a table

Build a table showing the % of babies born weighing under 2500g, broken down by race and smoking status.

```{r} # Edit me ```

##### (b) Nicer table output using `kable()`

Use the `kable()` function from the `knitr` library to display the table from part **(a)** in nice formatting.

```{r} library(knitr) # load the package

# Edit me ```

**Hint**: You will have to set the `results` argument in the R markdown code chunk, and also specify the appropriate `format` argument in the `kable()` function. The notes for Lecture 5 have a working example of this.

##### (c) Hiding code with `echo` Build a table showing the mean birth weights of babies, broken down by smoking status. Then run an approproiate statistical test test.

```{r} # Edit me ```

##### (d) Hiding code with `echo`

Repeat part **(b)**, but this time set the `echo` argument of the code chunk in such a way that the code is not printed, but the table is still displayed.

```{r} # Edit me ```

### Problem 2: A few simple plots

For this problem we'll use the `diamonds` dataset from the `ggplot2` package.

```{r} library(ggplot2) # Needed for ggplot2 graphics. Remember that the `ggplot2` library comes with a dataset called `diamonds`. ```

##### (a) Base R graphics

Use the `hist` function to create a histogram of `carat` with bars colored `steelblue`. ```{r} # Edit me ```

##### (b) qplot histogram

Use the `qplot` function from the `ggplot2` package to create a histogram of `depth`. Note that `geom = "histogram"` is a valid geometry in `qplot`.

```{r} # Edit me ```

##### (c) qplot boxplot

Use the `qplot` function from the `ggplot2` library to create boxplots showing how `price` varies across diamond `cut`. Specify `fill = cut` to get all the boxplots to be coloured differently.

```{r} # Edit me ```

**Hint**: For this exercise, it will be useful to know that `boxplot` is a geometry (`geom`) built into `ggplot2`, and that `qplot` can be called with the arguments: ```{r, eval = FALSE} qplot(x, y, data, geom, fill) ```

### Problem 3: ggplot practice

For this exercise we'll go back to the Cars93 data set in the MASS library

##### (a) size mapping, geom_point()

Define a `ggplot` object using the Cars93 data set that you can use to view `Price` on the y-axis, `EngineSize` on the x-axis, and set the `size` mapping to be based on `Horsepower`.

Use `geom_point()` to create a scatterplot from your `ggplot` object.

```{r} # Edit me ```

##### (b) colour mapping

Repeat part (a), this time also setting the `colour` mapping to be based on `Origin`.

```{r} # Edit me ```

##### (c) changing color palette

Repeat part (b), this time using the `scale_colour_manual()` layer to specify that you want to use `cbPalette` as your color palette.

```{r} cbPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")

# Edit me ```

### Problem 4: More ggplot2 practice

#####(a) stat_smooth()

Repeat part 4(b), this time using `stat_smooth()` to add a layer showing the smoothed curve representing how `Price` varies with `EngineSize`.

```{r} # Edit me ```

#####(b) facet_wrap()

Use your ggplot object from 4(b) along with the `geom_point()` and `facet_wrap()` layers to create scatterplots of `Price` against `EngineSize`, broken down by (conditioned on) `Origin`.

```{r} # Edit me ```

(Your code should produce a figure with two scatterplots, analogous to the `facet_wrap` example from class. Note that the example from class had a factor with 7 levels, so 7 scatterplots were produced. `Origin` has two levels.)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!