# A simple test of R Markdown and knitr

## Simulate data that have a linear relationship.

First, it"s good form to specify any constants, rather than embedding numbers in the code, so we"ll specify the sample size (n), slope, and intercept, mean of x, and standard deviation of x explicitly.

Next, we generate the x-values from a normal distribution with a specified mean and standard deviation.

To have scatter around the line, we need to specify errors (deviations) for each of our x points. Again, we specify the mean and standard deviation of our errors explicitly, which makes our code self-commenting. The goal here is not to have any unexplained constants appear in our code.

Last, we generate our y-values as being our x-values multiplied by the slope, with the intercept and errors added to them.

```{r simulateData}
n <- 50
slope <- 3.1
intercept <- 7.2

xMean <- 40
xSd <- 20
x <- rnorm(n, xMean, xSd)

errorMean <- 0
errorSd <- 20
errors <- rnorm(n, errorMean, errorSd)

y <- slope * x + intercept + errors
```

## Plot the data.

This will be a simple bivariate plot. The symbols will be set to small black circles (pch=16) to make them easier to see. The y-axis labels will also be rotated (las=1) to make them easier to read.

```{r dataPlot}
plot(x, y, pch=16, las=1)
```

## Perform a regression.

Regressions are done using the lm() function. The syntax of "y~x" should be read as "y as a function of x".

```{r regression}
myRegression <- lm(y~x)
```

View a simple summary of the regression by displaying the regression object you created.

```{r regressionObject}
myRegression
```

The regression object is a list with several named elements. To see what these are, use names().

```{r regressionList}
names(myRegression)
```

To view any of these, use $ notation. For example, you can display the regression coefficients.

```{r regressionCoefficients}
myRegression$coefficients
```

Our calculated slope (`r unname(round(myRegression$coefficient[2], 1))`) is close to what we specified (`r slope`), but our calculated intercept (`r unname(round(myRegression$coefficient[1], 1))`)  is rather different from what we set (`r intercept`), which is not unusual when most of the data is far from the y-axis.

Use abline() to add the line of regression to an existing plot.

```{r regressionLine}
plot(x, y, pch=16, las=1)
abline(myRegression, lwd=2, col="blue")
```

