-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJuly26_2021_examples.R
More file actions
60 lines (36 loc) · 1.56 KB
/
July26_2021_examples.R
File metadata and controls
60 lines (36 loc) · 1.56 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 regression
size <- c(3, 3.1, 2, 1.5, 2.3, 3.5, 3.6, 2.1)
protein <- c(1, 1.25, 1.5, 1.75, 2, 2.25, 2.5, 1.85)
plot(y = size, x = protein)
mod1 <- lm(size ~ protein)
abline(mod1)
summary(mod1) # estimated intercept, and slope
model.matrix(mod1) # design matrix for the simple regression
model.matrix(~protein) # the design matrix ONLY depends on the predictors.
## second example with two continuous predictors.
# now we have carbohydrates as well.
carbs <- c(0, 0.25, 0.5, 0.75, 1, 1.25, 1.5, 0.85)
mod2 <- lm( size ~ 1 + protein + carbs)
model.matrix(mod2)
# third example with a single discrete predictor (to demonstrate indicator/dummy variable coding)
# our predictor
water <- c("lake", "lake", "lake", "pond", "pond", "pond")
# our response variables (we model them seperately)
Nitrogen <- c(2,2.5, 2.1, 3.1, 3.1, 2.8 )
Phos <- c(2,2.5, 2.1, 1.6, 1.1, 1.8 )
mod_water <- lm(Nitrogen ~ 1 + water) # remember the "1" is there by default, so even Nitrogen ~ water gives same model
summary(mod_water)
model.matrix(mod_water) # treatment contrast coding.
#means of each group of water
tapply(Nitrogen, water, mean)
## same idea with a second response, but same predictor (design matrix does not change)
mod_water_P <- lm(Phos ~ 1 + water)
summary(mod_water_P)
tapply(Phos, water, mean)
model.matrix(mod_water_P)
# how about cell means model.
# just need to suppress the global intercept.
# note, this is not the real way of doing cell means, but it is fine for now.
mod_water_cell <- lm(Nitrogen ~ 0 + water)
summary(mod_water_cell)
model.matrix(mod_water_cell)