## R Programming Solution for Reading the Data Task

- 17th Nov, 2022
- 17:42 PM

```{r setup, include=FALSE}

knitr::opts_chunk$set(echo = TRUE)

```

## Importing libraries

```{r}

library(readxl)

library(strucchange)

```

## Reading the data

```{r}

df <- read_excel("OSCAR_R Language_Data.xlsx")

head(df)

```

```{r}

t.test(Number_Incidents ~ Walker...4, data = df)

```

```{r}

oscar.table <- with(df, table(Walker...4, Mailman))

oscar.table

```

```{r}

addmargins(oscar.table)

```

To obtain the appropriate row or column percents, we can use the prop.table() function on the table constructed, with a second argument margin= to specify showing row percents (1) or column percents (2) in the table.

```{r}

# To show the row percents

prop.table(oscar.table, margin=1)

```

```{r}

prop.table(oscar.table, margin=2)

```

```{r}

chisq.test(oscar.table)$expected

```

Based on the expected counts, the Chi-square methods be used to obtain a good approximation of the p-value for the Test for an Association.

```{r}

barplot(oscar.table,

beside=TRUE,

legend.text=TRUE)

```

```{r}

chisq.test(oscar.table)

```

There are two “tests of association” that R does. We’ll be using the Pearson Chi-square test. Give the -statistic, degrees of freedom, and p-value from the Pearson Chi-square test.

Test-statistic = X-squared = 2.6651e-30

df = (R-1)(C-1)=1

p-value = 1

```{r}

fisher.test(oscar.table)

```

he Fisher’s Exact Test gives an exact p-value for a Test for Association. However, R (and most other statistical software) only do the exact calculations for the Fisher’s Exact Test for 2x2 tables of counts (2 rows and 2 columns). An approximation for tables larger than 2x2 can be done but will not be shown or discussed in this class.

Note also that the fisher’s exact test output gives a confidence interval and sample estimate for the odds ratio. We do not discuss the odds ratio in this class.

No, there is suggestive evidence that there is an association between walker type and dog infection based on a p-vale of 1. Therefore, we will fail to reject the null at an \( \alpha=0.05 \) level.

```{r}

new.table <- with(df, table(Number_Incidents))

new.table

```

```{r}

prop.table(new.table)

```

```{r}

barplot(new.table,

legend.text=TRUE)

```

There is a hypothesized set of proportions

The data is categorical

There are at least 1 in each expected counts

```{r}

sctest(Walk_Length ~ Walker...4, data=df, type="Chow", point=44)

```