forked from ahulman/Assignment1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch2_jakob.R
69 lines (49 loc) · 1.96 KB
/
ch2_jakob.R
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
###Creates SimCohort
simCohort <- function(N,S){
set.seed(S)
###Jakob
id <- c(1:N)
#creates a sex variable for first 600 and appends last 400
sex <- rep.int(0,N*6/10)
sex <- append(sex, rep.int(1,N*4/10))
#creates dataframe from SEX and ID vectors
dataContinuous = data.frame("sex" = sex, "id" = id)
dataContinuous$age <- runif(N, min = 40, max = 70)
dataContinuous$bmi <- ifelse(dataContinuous$sex==0, 21 + 0.1*dataContinuous$age + rnorm(1, 0 , 2), 20 + 0.15*dataContinuous$age + rnorm(1, 0 , 2.5))
###OMAR
#Output data frame
dataCategorical = data.frame(sex, id)
#Generate ethnicity
dataCategorical$ethnic <-rbinom(N, 1, 0.05)
#Generate smoking status
smoke <- c(0, 1, 2)
dataCategorical$smoke <- ifelse(dataCategorical$sex == 0, sample(smoke, 1, replace = TRUE, prob = c(0.5, 0.3, 0.2)), sample(smoke, 1, replace = TRUE, prob = c(0.6, 0.3, 0.1)))
dataCategorical$smoke <- factor (dataCategorical$smoke,
levels = c(0, 1, 2),
labels = c("never", "ex", "current"))
###Analysis
total <- merge(dataContinuous,dataCategorical, by=c("id","sex"))
#change numeric storage type to factor
total$ethnic <- factor (total$ethnic,
levels = c(0, 1),
labels = c("non-white", "white"))
total$sex <- factor (total$sex,
levels = c(0, 1),
labels = c("male", "female"))
return(total)
}
#creates sample cohorts
simCohort(20000,1)
cohort1 <- simCohort(100, 123)
cohort2 <- simCohort(10000, 987)
#plots associations
plot(cohort1$age,cohort1$bmi)
plot(cohort1$sex,cohort1$bmi)
plot(cohort2$age,cohort2$bmi)
plot(cohort2$sex,cohort2$bmi)
#creates linear models
bmi.mod <- lm(formula = bmi ~ age, cohort1)
bmi.mod
bmi.mod <- lm(formula = bmi ~ sex, cohort1)
bmi.mod
plot(bmi.mod)