R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

## Generate example data
set.seed(786) 
x <- rnorm(1000) 
y <- exp(2*x + rnorm(1000, sd = 0.5))

# Summarize the model
model <- lm(log(y) ~ x) 
summary(model)
## 
## Call:
## lm(formula = log(y) ~ x)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.68041 -0.33637 -0.00092  0.33715  1.88287 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 0.004389   0.015743   0.279     0.78    
## x           1.999007   0.015937 125.433   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.4978 on 998 degrees of freedom
## Multiple R-squared:  0.9404, Adjusted R-squared:  0.9403 
## F-statistic: 1.573e+04 on 1 and 998 DF,  p-value: < 2.2e-16
# Predicted values
y_pred <- exp(predict(model))

# R-squared
rsq <- summary(model)$r.squared 
cat("R-squared:", rsq, "\n")
## R-squared: 0.9403515
# Mean squared error
mse <- mean((y - y_pred)^2) 
cat("Mean squared error:", mse, "\n")
## Mean squared error: 140.1547

Including Plots

library(ggplot2) 
# X vs. Y 
ggplot(data.frame(x, y), aes(x, y)) + geom_point() + labs(x = "x", y = "y") + theme_bw()

# Observed vs. predicted values
ggplot(data.frame(y, y_pred), aes(y, y_pred)) + geom_point() + geom_abline(intercept = 0, slope = 1, linetype = "dashed") + labs(x = "Observed", y = "Predicted") + theme_bw()

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.