---
title: 'Stat. 650 Section 1 or 2: Homework 1'
output:
  word_document: default
  pdf_document: default
  html_notebook: default
  html_document:
    df_print: paged
---

**Prof. Eric A. Suess**

So how should you complete your homework for this class?

- First thing to do is type all of your information about the problems you do in the text part of your R Notebook.
- Second thing to do is type all of your R code into R chunks that can be run.
- If you load the tidyverse in an R Notebook chunk, be sure to include the "message = FALSE" in the {r}, so {r message = FALSE}.
- Last thing is to spell check your R Notebook.  Edit > Check Spelling... or hit the F7 key.

# Homework 1

    Read: Preface, Chapter 1, Appendix A, B, and D. 
    Problems:
    Appendix B Exercises B.1, B.2, B.3, B.4
    Appendix D Exercises D.1, D.6, D.11

\newpage

# Appendix B

## B.1  

We see nothing.  Well actually we see the first layer of a ggplot2 plot.

```{r message = FALSE}
obj1 <- 2:10
obj2 <- c(2, 5)
obj3 <- c(TRUE, FALSE)
obj4 <- 42

obj1 * 10
obj1[2:4]
obj1[-3]
obj1 + obj2
obj1 * obj3
obj1 + obj4
obj2 + obj3
sum(obj2)
sum(obj3)
```

\newpage

# B.2 

```{r}
a <- c(10, 15)
b <- c(TRUE, FALSE)
c <- c("happy", "sad")
```

```{r}
data.frame(a, b, c)
```

```{r}
cbind(a, b)
```

```{r}
rbind(a, b)
```

```{r}
cbind(a, b, c)
```

```{r}
list(a, b, c)[[2]]
```

\newpage

# B.3 

```{r}
mylist <- list(x1="sally", x2=42, x3=FALSE, x4=1:5)
```

```{r}
is.list(mylist)
names(mylist)
length(mylist)
mylist[[2]]
mylist[["x1"]]
mylist$x2
length(mylist[["x4"]])
class(mylist)
typeof(mylist)
class(mylist[[4]])
typeof(mylist[[3]])
```

\newpage

# B.4

```{r}
library(mosaic)
ds <- read.csv("http://nhorton.people.amherst.edu/r2/datasets/helpmiss.csv")
ds
```

Describe in words what computations are being done. Using the “pipe” notation, trans-
late this code into a more readable version.

```{r}
summarise(group_by(select(filter(mutate(ds,
  sex = ifelse(female==1, "F", "M")), !is.na(pcs)), age, pcs, sex),
  sex), meanage=mean(age), meanpcs=mean(pcs),n=n())
```

**Answer of exercise B.4**

This code generates a new variable called sex based on the value of the female variable,
drops any observations missing the pcs variable, selects three variables, then displays the
average age, PCS, and sample size for each of the two groups defined by sex.

Here it is much clearer what each operation within the “pipe-stream” is doing. It is
straightforward to debug expressions in this manner by just leaving off the %>% at each line:
this will only evaluate the set of functions called to that point and display the intermediate
output.

```{r}
library(mosaic)
ds <-
read.csv("http://nhorton.people.amherst.edu/r2/datasets/helpmiss.csv")
ds %>% mutate(sex = ifelse(female==1, "F", "M")) %>%
  filter(!is.na(pcs)) %>%
  select(age, pcs, sex) %>%
  group_by(sex) %>%
  summarise(meanage = mean(age), meanpcs = mean(pcs), count = n())

```

\newpage

# B.5

Answers may vary. 

```{r eval=FALSE}
library(readr) # package, function call, argument
ds <- read_csv("http://www.mysite.com/myfile", progress = TRUE)
# quoted character string, named argument
```

\newpage

# Appendix D

## D.1

```{r}
x <- 1:5
```

```{r}
x <- x + 1
```

```{r}
x
```


\newpage

## D.6

The implication is that arbitrary
LATEX commands can be included in R Markdown files to format mathematical expressions.

$\hat{y} = \hat{\beta}_0 + \hat{\beta}_1 \cdot x + \epsilon$

\newpage

## D.11


```{r results="asis"}
library(xtable)
library(mdsr)
options(xtable.comment = FALSE)
mod <- lm(cesd ~ mcs + sex, data = HELPrct)
xtable(mod)
```


