-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSLR_model.qmd
More file actions
60 lines (43 loc) · 1.33 KB
/
SLR_model.qmd
File metadata and controls
60 lines (43 loc) · 1.33 KB
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
# Simple linear regression model: Example
## Read data
```{r}
senic <- read.table('./Datasets/SENIC_data.txt')
```
## Data pre-processing
```{r}
colnames(senic) <- c("ID", "LOS", "AGE", "INFRISK", "CULT", "XRAY", "BEDS", "MEDSCHL", "REGION", "CENSUS", "NURSE", "FACS")
```
## Model
Develop a linear regression model to Predict the length of stay based on probability of the person getting infected.
```{r}
model <- lm(LOS~INFRISK, data = senic)
summary(model)
```
```{r}
library(ggplot2)
ggplot(data = senic, aes(x = INFRISK, y = LOS))+
geom_point()+
geom_smooth(method = "lm")
```
## Error variance
```{r}
sum(model$residuals**2)/(111)
```
## Confidence and Prediction intervals
```{r}
library(ggplot2)
ci <- predict(model, newdata = senic, interval = "confidence", level = 0.95)
pi <- predict(model, newdata = senic, interval = "prediction", level = 0.95)
data_ci <- cbind(senic, ci)
data_pi <- cbind(senic, pi)
ggplot(data = senic, aes(y = LOS, x = INFRISK))+
geom_point()+
geom_line(data = data_ci, aes(x = INFRISK, y = lwr), color = "red")+
geom_line(data = data_ci, aes(x = INFRISK, y = upr), color = "red")+
geom_line(data = data_pi, aes(x = INFRISK, y = lwr), color = "green")+
geom_line(data = data_pi, aes(x = INFRISK, y = upr), color = "green")+
geom_smooth(method = "lm")+
labs(
y = "Length of stay"
)
```