-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLab3.Rmd
74 lines (65 loc) · 1.46 KB
/
Lab3.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
---
title: "R Notebook"
output: html_notebook
---
```{r}
library(pacman)
p_load(psych,ggplot2, lm.beta, dplyr,foreign)
```
**Load data into R**
```{r}
rm(list = ls())
df<-read.spss("Class_Survey.sav", to.data.frame=T)
```
```{r}
cor.test(df$friends1, df$friends2)
```
Since correlation between friends1 and friends2 is strong, we average the friends 1 and friends2 variables.
```{r}
df<- df %>% mutate(friends = (friends1+friends2)/2)
```
Computate the descriptive statistics
```{r}
df %>% select(depress,friends) %>% describe()
```
Plot the histogram
```{r}
ggplot(df, aes(x= friends))+geom_histogram()
```
```{r}
ggplot(df, aes(x=friends,y=depress))+ geom_point()+ geom_smooth(method = lm, fullrange=T) + xlim(c(0,7))
```
```{r}
mod1 <- lm(depress~friends, df)
summary(lm.beta(mod1))
```
```{r}
confint(mod1, level = 0.95)
```
```{r}
anova(mod1)
```
Compute multiple R from the anova table with ss. Check same as the lm.beta results.
```{r}
(14.686)/(14.686+245.677)
```
**Centering variabels**
```{r}
df <- df%>% mutate(friends.c = friends - mean(friends))
df <- df%>% mutate(friends.c = scale(friends, scale = F))
```
```{r}
mod2 <- lm(depress ~ friends.c, df)
summary(lm.beta(mod2))
```
```{r}
ggplot(df, aes(x=friends.c, y=depress))+ geom_point()+ geom_smooth(method = lm, fullrange=T)
```
**Multiple regression**
```{r}
mod3<- lm(depress~friends+beer, df)
summary(lm.beta(mod3))
```
```{r}
anova(mod3)
```