my_dt <- read.delim(textConnection(dt), header = TRUE, sep = "\t", stringsAsFactors = FALSE)
library(tidyverse)
my_dt <- my_dt %>%
mutate(Weight = as.numeric(Weight),
Height = as.numeric(Height),
Abdomen = as.numeric(Abdomen))
## Warning: NAs introduced by coercion
my_dt %>%
summarise_all(.funs = c("mean", "sd"), na.rm = TRUE)
## Weight_mean Height_mean Abdomen_mean Weight_sd Height_sd Abdomen_sd
## 1 36.90441 1.408476 65.34031 11.15982 0.09828225 10.44111
library(ggplot2)
ggplot(my_dt) +
geom_histogram(aes(x = Height))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 1 rows containing non-finite values (stat_bin).
ggplot(my_dt) +
geom_histogram(aes(x = Weight))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
my_dt %>%
filter(Height >= 1.6)
## Weight Height Abdomen
## 1 51.70 1.65 70.33
## 2 94.60 1.60 112.83
## 3 63.45 1.61 83.33
## 4 37.75 1.60 67.00
## 5 46.30 1.61 66.00
## 6 54.10 1.64 75.00
## 7 88.10 1.76 96.33
## 8 49.85 1.62 64.50
## 9 47.75 1.65 66.33
my_dt %>%
mutate(ct_above16 = if_else(Height >= 1.6, 1, 0)) %>%
summarise(
n = n(),
prob = sum(ct_above16, na.rm = TRUE),
prob_above16 = prob / n * 100
)
## n prob prob_above16
## 1 247 9 3.643725
my_dt %>%
mutate(ct_above59 = if_else(Weight > 59, 1, 0)) %>%
summarise(
n = n(),
prob = sum(ct_above59, na.rm = TRUE),
prob_above59 = prob / n * 100
)
## n prob prob_above59
## 1 247 10 4.048583
mylm1 <- lm(formula = Weight ~ Height, data = my_dt)
summary(mylm1)
##
## Call:
## lm(formula = Weight ~ Height, data = my_dt)
##
## Residuals:
## Min 1Q Median 3Q Max
## -15.657 -4.586 -1.542 3.283 41.193
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -84.671 6.692 -12.65 <2e-16 ***
## Height 86.299 4.740 18.21 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 7.291 on 244 degrees of freedom
## (1 observation deleted due to missingness)
## Multiple R-squared: 0.576, Adjusted R-squared: 0.5743
## F-statistic: 331.5 on 1 and 244 DF, p-value: < 2.2e-16
Group 6: abdomen vs weight
mylm2 <- lm(formula = Weight ~ Abdomen, data = my_dt)
summary(mylm2)
##
## Call:
## lm(formula = Weight ~ Abdomen, data = my_dt)
##
## Residuals:
## Min 1Q Median 3Q Max
## -12.8999 -3.1363 -0.3356 2.1786 20.6240
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -27.65741 1.98709 -13.92 <2e-16 ***
## Abdomen 0.98758 0.03003 32.88 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.693 on 223 degrees of freedom
## (22 observations deleted due to missingness)
## Multiple R-squared: 0.829, Adjusted R-squared: 0.8283
## F-statistic: 1081 on 1 and 223 DF, p-value: < 2.2e-16
residuals(mylm2) %>%
enframe() %>%
kable()
| name | value |
|---|---|
| 1 | -2.1544180 |
| 2 | -3.3912515 |
| 3 | -2.4480150 |
| 4 | -0.1280130 |
| 5 | 0.6474554 |
| 6 | -2.4159033 |
| 7 | -2.0907472 |
| 8 | -3.5530489 |
| 9 | -1.8090678 |
| 10 | -4.0089477 |
| 11 | -1.4783252 |
| 12 | -4.2162155 |
| 13 | 1.2898753 |
| 14 | -5.2462135 |
| 15 | -1.6283252 |
| 16 | -6.5275806 |
| 17 | -0.9286374 |
| 18 | -3.3907472 |
| 19 | 8.7904279 |
| 20 | 0.2534743 |
| 21 | -2.5480150 |
| 22 | 0.0027296 |
| 23 | -0.7974624 |
| 24 | -0.8355931 |
| 25 | -0.5968380 |
| 26 | 1.3906199 |
| 27 | 0.5775735 |
| 28 | 0.9530419 |
| 29 | -1.3831671 |
| 30 | -5.8472704 |
| 31 | 1.2389386 |
| 32 | 0.8519850 |
| 33 | -0.8592599 |
| 34 | -5.5223063 |
| 35 | 3.1913646 |
| 36 | -1.7850405 |
| 37 | -6.0103168 |
| 38 | 2.5289426 |
| 39 | 4.5642149 |
| 40 | -3.4159033 |
| 41 | -9.1730510 |
| 42 | -3.9475826 |
| 43 | -5.6734834 |
| 44 | -2.1968380 |
| 45 | -3.2089477 |
| 46 | 2.6092528 |
| 47 | -0.6788295 |
| 48 | -1.8148464 |
| 49 | 2.1085082 |
| 50 | -12.8998884 |
| 51 | -2.5989517 |
| 52 | -1.9165277 |
| 53 | -0.7727387 |
| 54 | 1.2537865 |
| 55 | 2.0522253 |
| 56 | 4.5722993 |
| 57 | 3.5526095 |
| 58 | 0.1530419 |
| 59 | -6.1486395 |
| 60 | 4.6778858 |
| 61 | -0.1859053 |
| 62 | -3.6353527 |
| 63 | -2.3037936 |
| 64 | 0.9034743 |
| 65 | -6.3723063 |
| 66 | 2.8034743 |
| 67 | -0.2965257 |
| 68 | 1.3913646 |
| 69 | 0.6278858 |
| 70 | -0.0965257 |
| 71 | 4.5392508 |
| 72 | -0.2859053 |
| 73 | -6.0873463 |
| 74 | 1.7406199 |
| 75 | -8.2266057 |
| 76 | -5.8234834 |
| 77 | 4.2389386 |
| 78 | 1.2413646 |
| 79 | -1.2362175 |
| 80 | 4.0151516 |
| 81 | 0.9534743 |
| 82 | 4.0407401 |
| 83 | -1.4847283 |
| 84 | -0.9234834 |
| 85 | -2.9355931 |
| 86 | -5.7968380 |
| 87 | 1.2778858 |
| 88 | 0.9030419 |
| 89 | -0.3983272 |
| 90 | -2.5775806 |
| 91 | -0.6348484 |
| 92 | -1.4548504 |
| 93 | -0.2780130 |
| 94 | -1.4596923 |
| 95 | -2.9903147 |
| 96 | 0.3903077 |
| 97 | -10.4364096 |
| 98 | 2.3198014 |
| 99 | -4.9089477 |
| 100 | 3.9645271 |
| 101 | -3.6719941 |
| 102 | 10.7831600 |
| 103 | 3.6466388 |
| 104 | -9.3980869 |
| 105 | -5.7862894 |
| 106 | -3.4730510 |
| 107 | -5.0353527 |
| 108 | 0.0040988 |
| 110 | -0.4266459 |
| 111 | -3.5919893 |
| 112 | -0.2352808 |
| 113 | 0.7157761 |
| 114 | -6.7529838 |
| 115 | -6.1230510 |
| 117 | 5.7037865 |
| 119 | 3.8016728 |
| 120 | -4.0844160 |
| 121 | 2.4499330 |
| 122 | -2.9093801 |
| 123 | -4.5244785 |
| 124 | 5.1530419 |
| 125 | -0.1468380 |
| 127 | -3.4092599 |
| 128 | -5.8719941 |
| 129 | 5.7634743 |
| 130 | 0.9158962 |
| 131 | 1.0022972 |
| 133 | -8.2995762 |
| 134 | 7.2763381 |
| 135 | -4.4116859 |
| 136 | 2.6799377 |
| 137 | -3.1362759 |
| 138 | -3.7872126 |
| 139 | 11.3252812 |
| 140 | -3.0986395 |
| 141 | -5.9474624 |
| 142 | 1.4268289 |
| 143 | 0.7640228 |
| 145 | 2.9158962 |
| 147 | -3.2129348 |
| 148 | -0.1105089 |
| 149 | -2.8247323 |
| 150 | 8.3651516 |
| 151 | 1.6395631 |
| 152 | -1.8724265 |
| 153 | 0.2413646 |
| 154 | -1.6228589 |
| 155 | -4.7842239 |
| 156 | -6.6227387 |
| 157 | -4.4604369 |
| 158 | -3.8482071 |
| 159 | -4.6595721 |
| 160 | -1.2459012 |
| 161 | 1.8789426 |
| 162 | -5.0231711 |
| 163 | -1.7475826 |
| 164 | -0.7469581 |
| 165 | -0.3355931 |
| 166 | -2.5352808 |
| 167 | -2.2713696 |
| 168 | 0.2416768 |
| 169 | 3.8337845 |
| 170 | 5.5093730 |
| 171 | 4.2957021 |
| 172 | 5.8155840 |
| 173 | 1.8896832 |
| 174 | 2.3907401 |
| 175 | 9.9010483 |
| 176 | 5.0625335 |
| 177 | 10.8289808 |
| 178 | -1.1421882 |
| 179 | 15.8935884 |
| 180 | 5.6086284 |
| 181 | 0.3826557 |
| 182 | 4.9513605 |
| 183 | 7.7966388 |
| 184 | 8.8125335 |
| 185 | -1.8921163 |
| 186 | 6.1903077 |
| 187 | -0.3559792 |
| 188 | 5.2534743 |
| 189 | 0.0524174 |
| 190 | -1.3729308 |
| 191 | -0.7603168 |
| 192 | -4.2295742 |
| 193 | 1.9083161 |
| 194 | 0.1578118 |
| 195 | 2.5203057 |
| 196 | 8.7772613 |
| 197 | -3.5485193 |
| 198 | -3.2841038 |
| 199 | 1.8817909 |
| 200 | 7.6890587 |
| 201 | 1.3448373 |
| 202 | 5.5204258 |
| 203 | 20.6240187 |
| 204 | -0.6424285 |
| 205 | -7.7053548 |
| 206 | -0.2539856 |
| 207 | 13.8086284 |
| 208 | 0.0827276 |
| 209 | -3.8737956 |
| 210 | 2.8148393 |
| 211 | 12.8625335 |
| 212 | 10.0958942 |
| 213 | 2.1786304 |
| 214 | -0.8983272 |
| 215 | 5.0221072 |
| 216 | 2.7637825 |
| 217 | 4.4870652 |
| 218 | -1.5795742 |
| 219 | 1.2403077 |
| 220 | 2.8581960 |
| 221 | -2.3721142 |
| 222 | 9.9013605 |
| 223 | 1.4413646 |
| 224 | 2.1395631 |
| 225 | 1.7403077 |
| 226 | -1.0334793 |
| 227 | -4.8735553 |
| 228 | -5.2472704 |
| 229 | -1.3962135 |
| 230 | -4.2462135 |
| 231 | 1.2031620 |
| 232 | -1.2086354 |
my_dt %>%
filter(!is.na(Abdomen)) %>%
nrow()
## [1] 225
confint(mylm2, level = 0.95)
## 2.5 % 97.5 %
## (Intercept) -31.5732820 -23.741544
## Abdomen 0.9283952 1.046761
plot(mylm2)
dt2 <- "
20 0.3 0.385
40 0.3 0.621
60 0.3 0.777
80 0.3 0.873
100 0.3 0.930
20 0.5 0.799
40 0.5 0.973
60 0.5 0.997
80 0.5 0.999
100 0.5 0.999
"
my_dt2 <- read.delim(textConnection(dt2), stringsAsFactors = FALSE, header = FALSE, sep = " ")
gg <- my_dt2 %>%
rename(sample_size = V1,
effect_size = V2,
power = V3) %>%
mutate(effect_size = factor(effect_size))
ggplot(gg) +
geom_line(aes(x = sample_size,
y = power,
color = effect_size))