-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlatt_1.Rmd
165 lines (129 loc) · 2.89 KB
/
Blatt_1.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
---
title: "Blatt 1"
author: "Vanessa Kleisch"
date: "2024-04-18"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(readr)
library(ggplot2)
library(patchwork)
library(dplyr)
library(data.table)
```
## Aufgabe 1
### 1a
ALternative siehe Lösungsblatt
```{r}
data1 <- read_rds("RunningAgg.Rds")
m1 <- ggplot(data1, aes(x = pace, y = HR)) +
geom_point()+
geom_smooth(method = "lm")
m1
```
## 1b
```{r}
head(data1)
data1$speed <- 60/data1$pace
m2 <- ggplot(data1, aes(x = speed, y = HR)) +
geom_point()+
geom_smooth(method = "lm")
m2
# beide :
m1 | m2
```
## 1d
```{r}
data1d <- data1 %>% mutate(HRbps = HR * 1/60, speedMi = pace* 1/ 1.61)
head(data1d)
```
## 2
### 2b
```{r}
set.seed(467)
# ist die n = 10 wichtig?
X <- runif(10000, 0, 10)
Y <- -2 +3.5*X + rnorm(10000, 0, sqrt(10))
variance2 <- 10
n <- 10000 # ANzhal Datenpunkte
simdata <- data.frame(X, Y)
head(simdata)
```
```{r}
# Varianz Dach (warum ist ds jetzt die wahre? die Werte sind doch geschätzt?)
b1dach <- 10/(n*var(X))
b1dach
b0dach <- 10*((1/n)+ ((mean(X))^2)/n*var(X))
b0dach
```
Jetzt zu ermitteln: wie ist beta0 und beta1 verteilt? -> aus Daten lm Modell fitten (10 000 mal wiederholen)
```{r}
reps <- 10000
fit <- matrix(ncol = 2, nrow = reps)
for (i in 1:reps) {
# wählt zufällig aus unsren Daten 10 Datenpunkte heraus (Zeilen), aber why 10?
# -> Anzahl an x Werten
sample_data <- simdata[sample(1:10000, 10),]
# Aus diesen Daten ein lineares Modell fitten & Koeffizientenj beta extrahieren
fit[i, ] <- lm(X~Y, data = sample_data)$coefficients
}
head(sample_data)
head(simdata)
head(fit)
```
Grafischer Vergleich
```{r}
par(mfrow = c(1, 2))
# Achsenabschnitt:
hist(x = fit[, 1], cex.main = 1,
# titel & Achsen
main = bquote(Distribution ~ of ~ 10000 ~ beta[0] ~ estimates),
xlab = bquote(hat(beta)[0]), freq = FALSE)
curve(dnorm(x = x, mean = -2, sd = sqrt(b0dach)), add = TRUE,
col = "darkred")
# Steigungsparameter:
hist(x = fit[, 2], cex.main = 1,
main = bquote(Distribution ~ of ~ 10000 ~ beta[1] ~ estimates),
xlab = bquote(hat(beta)[1]), freq = FALSE)
curve(dnorm(x = x, mean = 3.5, sd = sqrt(b1dach)), add = TRUE,
col = "darkred")
```
## 3
### a
```{r}
data3 <- data.frame("Größe" = c(198, 188, 196, 190, 180, 183, 196, 196, 193, 183),
"Gewicht" = c(104, 84, 107, 95, 76, 79, 109, 94, 113, 93))
ggplot(data3, aes(Größe, Gewicht)) +
geom_point()
```
positiver Zusammenhang erkennbar
### b
```{r}
y_strich <- mean(data3$Gewicht)
gewicht <- data3$Gewicht
größe <- data3$Größe
x_strich <- mean(größe)
y_strich
sst <- sum((gewicht - y_strich)^2)
sst
```
### c
```{r}
n <- 10
beta1 <- cov(gewicht, größe)/var(größe)
beta0 <- y_strich - beta1*x_strich
beta0
beta1
```
### d
```{r}
y_dach <- größe*beta1 + beta0
sse <- sum((gewicht - y_dach)^2)
1 - sse/sst
```
### e
```{r}
model3e <- lm(Gewicht ~ Größe, data3)
summary(model3e)
```