Zum Inhalt springen

KnitR/diceIndependent.Rmd

Aus Wikiversity
---
title: "Checking for independence - a simple KnitR example"
author: "Martin Papke"
date: "23 August 2018"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(knitr)
library(readr)
```

# A simple KnitR example

## Data import

In this document we aim to show how KnitR can be used to gerenate a report or an article 
containing statistical data and how the R code can be integrated within the document.
As example data, we use 10000 dice rolls contained in the file *dice.csv*. As usual in R 
we can load the data with 
```{r loaddata}
  # data <- read.csv('dice.csv', stringsAsFactors=FALSE)
  # dice <- as.numeric(data$X3)  
```
To give a standalone example here, we use R's feature to generate random numbers 
```
  dice <- sample(1:6, 10000, replace=TRUE)
```

### Preperation of the data 
As we want to use the dice throws in pairs, we just generate a table comparing the even and the odd dice throws, 
this can be done as follows:
```{r dataprep}
even <- dice[seq.int(0,10000,2)] 
odd  <- dice[seq.int(1,10000,2)]
tbl  <- table(even, odd)
```
We obtain the results 
```{r table1, echo=FALSE}
  kable(tbl, caption='even and odd results compared')
```

## Statistics

No we check for independence, by invoking
```{r chisquared}
chi <- test.chisq(tbl)
p   <- chi$p.value
```
The $p$-value is `r p`. Hence, we can say that we have
```{r pvalue}
  if (p < 0.01) {
    "high significance for independene"
  } else if (p < 0.05) {
    "significance for independence"
  } else {
    "no significance for independence"
  }
```