---
output:
html_document: default
word_document: default
---
# Section 1
Examples of using R as a calculator and how to work with objects:
```{r section1}
2+2
pnorm(1.96)
(2-3)/6
2^2
sin(pi/2)
log(1)
save<-2+2
save
ls()
objects()
```
2 + 2 = `r save`
# Section 2
The sd2() function is a simple function that calculates the standard deviation for a set of numbers.
```{r section2}
x <- c(1,2,3,4,5)
sd2 <- function(numbers) {
sqrt(var(numbers))
}
sd2(x)
```
Alternative ways to write the function are shown in the next subsections.
## Alternative #1
Print information from within the function.
```{r alt1}
sd2 <- function(numbers) {
cat("Print the data \n", numbers, "\n")
sqrt(
var(numbers))
}
save <- sd2(x)
save
```
## Alternative #2
Calculate the standard deviation without the var() function.
```{r alt2}
sd3 <- function(numbers) {
sqrt(sum((numbers-mean(numbers))^2)/(length(numbers)-1))
}
sd3(x)
sd4 <- function(numbers) { cat("Print the data \n", numbers, "\n");
sqrt(var(numbers)) }
sd4(x)
```
## Alternative #3
Calculate the standard deviation with all of the function's code on one line. The semicolon is used to separate cat() and sqrt() function calls. This symbol is used to signal the end of a complete line of code (rarely is there a need for it).
```{r alt3}
sd4 <- function(numbers) { cat("Print the data \n", numbers, "\n"); sqrt(var(numbers)) }
sd4(x)
```
# Section 3
Calculate probabilities from a standard normal distribution.
```{r section3}
pnorm(1.96)
pnorm(q = 1.96)
pnorm(1.96, 0, 1)
pnorm(q = 1.96, mean = 0, sd = 1)
```
# Section 4
Calculate a confidence interval for a population mean.
```{r section4}
pnorm(q = c(-1.96,1.96))
qt(p = c(0.025, 0.975), df = 9)
x <- c(3.68, -3.63, 0.80, 3.03, -9.86, -8.66, -2.38, 8.94, 0.52, 1.25)
x
var.xbar <- var(x)/length(x)
mean(x) + qt(p = c(0.025, 0.975), df = length(x)-1) * sqrt(var.xbar)
t.test(x = x, mu = 2, conf.level = 0.95)
```
# Plot example
While not in the appendix initial examples, one can also include plots. Below is a simple histogram of 1,000 observations simulated from a standard normal distribution.
```{r plot}
set.seed(3278)
hist(x = rnorm(n = 100), freq = FALSE, xlab = "x")
curve(expr = dnorm(x = x), add = TRUE, col = "red", n = 1000)
```
# Equation example
Equations are included using a LaTeX code formant. For example, an inline equation representing an estimated regression model is
$\hat{Y}=1.0869+0.6125x$. An equation on its own line is
$$\hat{Y}=1.0869+0.6125x.$$
# Changing text attributes
This is bold text: **bold**
This is italicized text: *italicized*