You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Save to Figures directory, assuming this directory/folder already exists. You can also change the width/height of your figure and dpi (resolution/quality) of your figure (journals usually expect around 300 dpi).
121
+
Save to Figures directory, assuming this directory/folder already exists. You can also change the width/height of your figure and dpi (resolution/quality) of your figure (since journals often expect around 300 dpi).
Test the relationship above by "controlling" for class
451
451
452
452
```{r}
453
-
modelLinear_class <- lm(iq ~ grades + class, data = df1)
454
-
summary(modelLinear_class) # get model results and p values
455
-
summaryh(modelLinear_class)
453
+
model_linear_class <- lm(iq ~ grades + class, data = df1)
454
+
summary(model_linear_class) # get model results and p values
455
+
summaryh(model_linear_class)
456
456
```
457
457
458
458
Note the significantly positive relationship between iq and grades now.
459
459
460
460
### Reference groups and releveling (changing reference group)
461
+
461
462
R automatically recodes categorical/factor variables into 0s and 1s (i.e., dummy-coding). Alphabets/letters/characters/numbers that come first (a comes before b) will be coded 0, and those that follow will be coded 1.
462
463
463
464
In our case, class "a" has been coded 0 (reference group) and all other classes ("b", "c", "d") are contrasted against it, hence you have 3 other effects ("classb", "classc", "classd") that reflect the difference between class "a" and each of the other classes.
@@ -478,30 +479,35 @@ summaryh(lm(iq ~ grades + class, data = df1)) # quickly fit model and look at ou
478
479
* concise expression: y ~ x1 * x2 (includes all main effects and interaction)
479
480
480
481
```{r}
481
-
modelLinear_interact <- lm(iq ~ grades + class + grades:class, data = df1)
482
-
summary(modelLinear_interact)
483
-
summaryh(modelLinear_interact)
482
+
model_linear_interact <- lm(iq ~ grades + class + grades:class, data = df1)
483
+
summary(model_linear_interact)
484
+
summaryh(model_linear_interact)
484
485
```
485
486
486
487
#### Intercept-only model
487
488
488
489
R uses `1` to refer to the intercept
489
490
490
491
```{r}
491
-
modelLinear_intercept <- lm(iq ~ 1, data = df1) # mean iq
492
-
# summaryh(modelLinear_intercept)
492
+
model_linear_intercept <- lm(iq ~ 1, data = df1) # mean iq
493
+
coef(model_linear_intercept) # get coefficients from model
494
+
# summaryh(model_linear_intercept)
493
495
df1[, mean(iq)] # matches the intercept term
494
496
mean(df1$iq) # same as above
495
497
```
496
498
497
-
Remove intercept from model (if you ever need to do so...) by specifying `-1`
499
+
Remove intercept from model (if you ever need to do so...) by specifying `-1`. Another way is to specify `0` in the syntax.
coef(lm(iq ~ 0 + grades, data = df1)) # no intercept
503
507
```
504
508
509
+
Be careful when you remove the intercept (or set it to 0). See [my article](https://hausetutorials.netlify.app/posts/2019-07-24-what-happens-when-you-remove-or-set-the-intercept-to-0-in-regression-models/) to learn more.
510
+
505
511
### Fitting ANOVA with `anova` and `aov`
506
512
507
513
By default, R uses Type I sum of squares.
@@ -511,7 +517,7 @@ Let's test this model with ANOVA.
511
517
```{r}
512
518
ggplot(df1, aes(class, iq)) + # y: iq
513
519
geom_quasirandom(alpha = 0.3) +
514
-
stat_summary(fun.y = mean, geom = 'point', size = 3) + # apply mean function to y axis (fun.y = mean)
520
+
stat_summary(fun = mean, geom = 'point', size = 3) + # apply mean function
515
521
stat_summary(fun.data = mean_cl_normal, geom = 'errorbar', width = 0, size = 1) # apply mean_cl_normal function to data
516
522
```
517
523
@@ -529,7 +535,7 @@ Class * gender interaction (and main effects)
529
535
```{r}
530
536
ggplot(df1, aes(class, iq, col = gender)) + # y: iq
dt1 <- lapply(files, fread) %>% bind_rows() %>% as.data.table() # read data, combine items in list, convert to data.table
82
+
dt1 <- bind_rows(lapply(files, fread)) %>% as.data.table() # read data, combine items in list, convert to data.table
83
83
```
84
84
85
-
Note and explanation: `lapply` loops through each item in `files` and applies the `fread` function to each item. `bind_rows` combines all the dataframes stored as separate items in the list, and `tbl_dt()` converts the merged dataframe into `data.table` and `tibble` class.
85
+
Note and explanation: `lapply` loops through each item in `files` and applies the `fread` function to each item. `bind_rows` combines all the dataframes stored as separate items in the list, and `as.data.table()` converts the merged dataframe into `data.table` class.
0 commit comments