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:

a <- c(22,30)
b <- c(26,16)
x <- data.frame(a,b)
chisq.test(x)
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  x
## X-squared = 2.8296, df = 1, p-value = 0.09254
y <- matrix(c(22,30,26,16),nc=2)
chisq.test(y,correct = FALSE)
## 
##  Pearson's Chi-squared test
## 
## data:  y
## X-squared = 3.5708, df = 1, p-value = 0.0588
## Effect of simulating p-values
x <- matrix(c(12, 5, 7, 7), ncol = 2)
chisq.test(x)$p.value           # 0.4233
## [1] 0.4233054
chisq.test(x)
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  x
## X-squared = 0.64112, df = 1, p-value = 0.4233
x <- matrix(c(39, 44, 8, 2), ncol = 2)
chisq.test(x)$p.value           # 0.4233
## Warning in chisq.test(x): Chi-squared approximation may be incorrect
## [1] 0.1014673
chisq.test(x)
## Warning in chisq.test(x): Chi-squared approximation may be incorrect
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  x
## X-squared = 2.6823, df = 1, p-value = 0.1015
x <- matrix(c(22,26, 30, 16), ncol = 2)
chi.t <- chisq.test(x)
chi.t$expected
##          [,1]     [,2]
## [1,] 26.55319 25.44681
## [2,] 21.44681 20.55319
chi.t$observed
##      [,1] [,2]
## [1,]   22   30
## [2,]   26   16
chi.t
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  x
## X-squared = 2.8296, df = 1, p-value = 0.09254
chi.t <- chisq.test(x,correct = FALSE)
chi.t
## 
##  Pearson's Chi-squared test
## 
## data:  x
## X-squared = 3.5708, df = 1, p-value = 0.0588
x <- trunc(5 * runif(100))
x
##   [1] 4 1 2 3 2 2 0 0 0 1 2 1 3 2 1 0 0 0 2 3 0 4 0 3 2 3 0 3 0 3 3 0 2 4 1 4 0
##  [38] 3 3 4 3 2 2 0 2 3 4 0 1 4 2 1 4 3 4 4 1 0 3 3 2 3 3 4 0 3 2 4 2 2 1 2 1 3
##  [75] 4 1 2 0 4 1 4 3 1 4 2 3 0 1 3 0 2 1 0 1 0 1 3 0 4 0
table(x)
## x
##  0  1  2  3  4 
## 23 17 20 23 17
chisq.test(table(x)) 
## 
##  Chi-squared test for given probabilities
## 
## data:  table(x)
## X-squared = 1.8, df = 4, p-value = 0.7725
fit <- lm(weight~height,data=women)
summary(fit)
## 
## Call:
## lm(formula = weight ~ height, data = women)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.7333 -1.1333 -0.3833  0.7417  3.1167 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -87.51667    5.93694  -14.74 1.71e-09 ***
## height        3.45000    0.09114   37.85 1.09e-14 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.525 on 13 degrees of freedom
## Multiple R-squared:  0.991,  Adjusted R-squared:  0.9903 
## F-statistic:  1433 on 1 and 13 DF,  p-value: 1.091e-14
residuals(fit)
##           1           2           3           4           5           6 
##  2.41666667  0.96666667  0.51666667  0.06666667 -0.38333333 -0.83333333 
##           7           8           9          10          11          12 
## -1.28333333 -1.73333333 -1.18333333 -1.63333333 -1.08333333 -0.53333333 
##          13          14          15 
##  0.01666667  1.56666667  3.11666667
plot(women$weight~women$height)
abline(fit,col="red")

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

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