---
title: "lecture12"
output: html_document
---

```{r}
# Assessing the fit of a mean
# Example to Slide 4

# The data sample

politeness <- c(10, 10, 20, 20, 30, 30, 40, 40, 50, 50, 60, 60)
likability <- c(3, 8, 9, 10, 7, 9, 9, 11, 11, 12, 10, 13)

# The model

m <- mean(likability)
m

# Plotting

plot(politeness, likability)
abline(h = m)

# Residual Standard Error

sse <- sum((likability - mean(likability))**2)
df <-  (length(politeness) - 1)
s <- sqrt(sse / df)
s
```


```{r}
# Assessing the fit of a regression line
# Example to Slide 5

# The data sample

politeness <- c(10, 10, 20, 20, 30, 30, 40, 40, 50, 50, 60, 60)
likability <- c(3, 8, 9, 10, 7, 9, 9, 11, 11, 12, 10, 13)

# Fitting the linear model
m <- lm(likability ~ politeness)
m

# Plotting

plot(politeness, likability)
abline(m)

# Residual Standard Error

sse <- sum((likability - predict(m, data.frame(politeness)))**2)
df <-  (length(politeness) - 1)
s <- sqrt(sse / df)
s

# Predict Likability from Politeness = 45
predict(m, data.frame(politeness = 45))

```


```{r}
# Slide 11

height <- c(150, 150, 160, 160, 160, 175, 175, 180, 180)
weight <- c(50, 60, 65, 90, 80, 85, 75, 80, 90)

plot(height, weight)
m <- lm(weight ~ height)
abline(m)
m$coefficients

```



```{r}
# Slide 11

# Assessing the significance of the predictor
# Example to Slide 15

# The data sample

politeness <- c(10, 10, 20, 20, 30, 30, 40, 40, 50, 50, 60, 60)
likability <- c(3, 8, 9, 10, 7, 9, 9, 11, 11, 12, 10, 13)

# Building the linear model
m <- lm(likability ~ politeness)

# Print the statistics.
# It will tell you that the coefficient for politeness (our b1) is 0.1086,
# and that it is significantly different from 0. The t value is 3.541,
# and the p value 0.005593 (two-tailed test).
summary(m)

```

```{r}
# Slide 18

groups <- c(0, 0, 0, 0, 0, 1, 1, 1, 1, 1)
data <- c(5, 3, 2, 4, 2, 5, 8, 9, 10, 8)

# Running a Two-Sample t-Test 
t.test(data[6:10], data[1:5], var.equal = T)

# Building a Linear model
m <- lm(data ~ groups)

# Plotting
plot(groups, data)
abline(m)

# Printing the statistics
# You can see that the mean of group0 is 3.2 (the intercept) and that the coefficient for the group variable
# is 4.8. Thus, the mean of group1 is 3.2 + 4.8 = 8.0. Moreover, the t-Test yields the same t- and p value as the
# one using the t.test() procedure.
summary(m)
```

```{r}
# Slide 20

groups <- c(0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2)
data <- c(5, 3, 2, 4, 2, 5, 8, 9, 10, 8, 6, 8, 9, 8, 12)

# Running the ANOVA
a <- aov(data ~ groups)
summary(a)

# Building a Linear model
m <- lm(data ~ groups)

# Plotting
plot(groups, data)
abline(m)

# Printing the statistics
# You can see that the F value and the p value of the linear regression model 
# is equal to the F and p value from the anova.
summary(m)
```


