Question

The model of interest is a linear-regression model with two predictors, haemoglobin (g/dL) and serum cholesterol (mg/dL) that we know affect our outcome of interest – body mass index (BMI; kg/m2). We are interested to know how many individuals we are required to sample, in order to detect a change of 0.5 in BMI for individuals who received a particular treatment (e.g., an exercise program) compared to those who did not (i.e., “treatment” is the third predictor in the model). Equation below, presents the model of interest:

BMI = β0 + β1Hgb + β2Chol + β3*Treatment + ε

Where: + β0 = 18.06, β1= 0.297, β2=0.014; + Haemoglobin values (Hgb; g/dL) are normally distributed with a mean of 14.4 g/dL and standard deviation of 1.5 g/dL (i.e. N(14.4,1.5) distributed); + Serum Cholesterol values (Chol; mg/dL) are N(215,50) distributed; and + Treatment is an indicator variable, where 1 represents those that received the treatment, and 0 represents those that did not; and + The error term, ε ~ N(0,1).

  1. For this question,
  1. Write code that:
    1. randomly samples 100 individual’s haemoglobin and serum cholesterol from the above distributions§,
    2. randomly allocates exactly half of the subjects to the Treatment group, and
    3. calculates their BMI according to the above equation, assuming that β3=0.5.

Store the simulated haemoglobin, serum cholesterol, treatment, and BMI values in a data frame/dataset. §Note: To generate random normal values, you will need to use rnorm(). To randomly allocate individuals to each treatment group, you will need to use sample().

  1. Using your code from Part C Q1a, write a function which takes as input the value for the sample size, n, and returns an object/data frame containing n simulated individuals with their haemoglobin, serum cholesterol, treatment, and BMI values.
  1. Using your code from parts a and b, modify the code and include a for loop that:

  2. Simulates the model 1000 times; and

  3. fits a linear regression model* of “BMI ~ Hgb + Chol + Treatment” for each simulated data set, and counts the number of times that the p-value associated with Treatment is <0.05; and

  4. cycles through sample sizes between 40 and 200 in step sizes of 20 and performs (a) and (b) and stores/returns the proportion of simulated experiments that had a p- value for treatment <0.05. This is an estimate of the Power (i.e., 1 – Type II error rate). *Notes: To fit the linear regression model in R, you can use:

    lm(BMI ~ Hgb + Chol + Trt, data=df)

Make sure that you set the random number seed so that the results are reproducible.

  1. Create a figure showing the sample size on the x-axis, and the estimated Power on the y- axis. Include the following items in your figure:
  1. A horizontal line at 80% Power.

  2. Use your favourite colour (ensure that this is not the default colour) to highlight the point that corresponds to the smallest sample size such that “we have at least 80% power to detect a difference of 0.5 in treatment between the two groups at the 5% significance level”. You will need to automate this, and not manually select this value (Hint: you want to find the minimum sample size that has an estimated Power ≥80%).

  3. Change the y-axis to have labels from 20% to 100% power in steps of 10%. Change the x-axis to go from a sample size of 40 to 200 in steps of 40. Make sure the axes have appropriate labels.

Solution

#--------------------------
# 1
#-------------------------
n = 100
b0 = 18.06
b1 = 0.297
b2 = 0.014
## a
# 1 - a - 1
set.seed(2022)
Hgb <- rnorm(n, mean=14.4, sd=1.5)
Chol <- rnorm(n, mean=215, sd=50)
epsilon <- rnorm(n, mean=0, sd=1)
# 1 - a - 2
Treatment <- sample(c(0,1), size=100, prob=c(0.5,0.5), replace = T)
# 1 - a - 3
b3 = 0.5
BMI = b0 + b1*Hgb + b2*Chol + b3*Treatment + epsilon 
# store the simulated data
df <- data.frame(bmi=BMI, hgb=Hgb, chol=Chol, treatment=Treatment)

## b
simulate_data <- function(n){
  b0 = 18.06
  b1 = 0.297
  b2 = 0.014
  b3 = 0.5
  set.seed(2022)
  Hgb <- rnorm(n, mean=14.4, sd=1.5)
  Chol <- rnorm(n, mean=215, sd=50)
  epsilon <- rnorm(n, mean=0, sd=1)
  Treatment <- sample(c(0,1), size=n, prob=c(0.5,0.5), replace = T)
  BMI = b0 + b1*Hgb + b2*Chol + b3*Treatment + epsilon 
  df <- data.frame(bmi=BMI, hgb=Hgb, chol=Chol, treatment=Treatment)
  return(df)
}
simulate_data(n=500)
##          bmi       hgb      chol treatment
## 1   25.12694 15.750213 212.35791         1
## 2   26.54286 12.639981 240.62834         1
## 3   25.10047 13.053772 162.31710         1
## 4   24.41227 12.233248 189.44775         0
## 5   27.05235 13.903480 274.56931         1
## 6   24.14776 10.049057 149.53850         0
## 7   27.55383 12.811116 199.35078         1
## 8   22.53127 14.816932 202.23302         0
## 9   25.17493 15.524229 294.90889         0
## 10  27.99024 14.762374 242.85593         0
## 11  25.13902 15.909279 163.02917         1
## 12  28.51179 14.122281 191.76590         1
## 13  26.87822 12.927260 239.68633         0
## 14  24.80586 14.539362 212.21367         0
## 15  26.46931 14.320823 257.51184         0
## 16  24.17313 14.279508  97.01461         1
## 17  26.22904 13.418844 240.67956         0
## 18  24.82255 12.973975 188.80500         0
## 19  24.97944 15.929343 212.93683         0
## 20  24.12676 15.688570 209.04146         0
## 21  25.82677 14.946691 165.30374         1
## 22  26.26465 14.975477 227.09393         0
## 23  24.86658 16.070109 205.48847         0
## 24  27.81664 16.217265 298.12505         1
## 25  25.21722 13.877512 211.16308         1
## 26  24.70479 13.110670 180.60880         1
## 27  25.89369 15.375041 268.12742         1
## 28  26.80592 14.892089 208.12400         1
## 29  26.87685 13.623080 169.05866         1
## 30  25.67661 14.041527 210.02768         1
## 31  27.87679 14.576668 294.34492         1
## 32  27.14023 15.647278 191.16643         1
## 33  25.47028 12.061622 273.24845         1
## 34  24.57900 14.069221 207.79610         0
## 35  24.88387 13.174208 179.10065         0
## 36  27.52208 16.015016 228.96288         0
## 37  26.75532 16.019496 202.78367         0
## 38  24.25118 14.613192 197.02448         0
## 39  27.82632 14.635469 276.43649         1
## 40  24.87713 14.146920 176.27698         1
## 41  27.28721 13.996443 238.59048         1
## 42  25.30310 15.611653 226.23785         1
## 43  21.87393 12.712924 133.87104         0
## 44  23.81523 12.253818 197.72247         0
## 45  24.08607 14.490535 241.88040         0
## 46  25.75813 13.210526 185.04041         0
## 47  26.30597 14.910414 230.43212         1
## 48  26.17075 14.010797 222.97754         0
## 49  25.45835 12.442727 274.09508         0
## 50  25.08306 14.952260 158.08106         0
## 51  26.62039 16.939785 293.79321         0
## 52  24.80317 15.893756 135.19058         0
## 53  27.16724 14.680128 262.28356         0
## 54  28.07891 16.257506 319.45468         1
## 55  25.44699 14.864060 247.68251         0
## 56  26.18495 15.353577 249.89418         1
## 57  26.40906 14.434775 168.73013         1
## 58  25.94033 16.166795 221.39347         0
## 59  24.40747 13.719680 195.81778         0
## 60  26.45487 15.023891 190.75270         1
## 61  23.80299 12.392336 148.73038         0
## 62  23.22571 12.462038 192.70874         1
## 63  24.70963 13.936389 195.58639         0
## 64  24.97891 14.634768 172.28817         1
## 65  25.20963 13.149125 202.81510         1
## 66  27.08536 14.363176 279.21920         0
## 67  25.81796 12.693973 263.96319         1
## 68  24.77774 16.008081 192.76090         1
## 69  27.57601 17.871748 213.28831         1
## 70  25.60357 15.034460 181.20932         1
## 71  27.71422 14.194591 235.04531         1
## 72  26.11415 16.392595 168.02782         1
## 73  26.28365 15.054803 181.83112         1
## 74  25.01364 14.499643 227.86948         0
## 75  24.71555 16.356990 238.04497         0
## 76  25.46675 14.086111 182.02682         1
## 77  28.52671 15.927380 279.70852         1
## 78  25.76202 16.449052 185.44585         1
## 79  25.20227 16.615044 150.71046         0
## 80  27.20999 15.730960 270.11261         0
## 81  26.14446 12.876137 191.41845         1
## 82  26.21822 17.206304 243.18166         1
## 83  26.02068 16.014298 182.25876         1
## 84  24.73643 12.788389 179.66865         0
## 85  23.74992 11.106636 169.02565         1
## 86  27.69858 15.201817 234.53593         1
## 87  28.29847 16.415600 282.88463         1
## 88  27.93868 16.477505 201.86881         1
## 89  25.87495 18.520390 282.85236         1
## 90  23.47309 14.331083 222.44028         0
## 91  24.98898 15.514628 269.00950         0
## 92  26.78012 14.790637 256.20213         1
## 93  24.52912 15.042423 279.86864         1
## 94  25.54529 13.847612 274.89570         0
## 95  26.02619 18.731135 178.54533         1
## 96  26.01967 13.489391 205.05538         1
## 97  24.01067 11.581202 202.34870         1
## 98  26.94961 15.477535 196.98109         0
## 99  26.03338 14.777138 173.98172         0
## 100 27.11405 15.100534 278.37514         1
## 101 24.85311 14.073167 233.78677         0
## 102 26.17482 12.573528 207.62336         1
## 103 25.41286 13.268882 212.41006         1
## 104 26.32711 14.686845 187.96290         1
## 105 25.76923 15.970924 167.99604         1
## 106 24.05369 16.034743 192.69394         1
## 107 24.78739 15.443212 174.85641         1
## 108 24.56023 13.613447 204.50803         0
## 109 26.72097 14.796024 213.54635         1
## 110 26.34472 16.261801 188.45974         0
## 111 24.24901 15.364946 253.03779         0
## 112 25.13098 12.904856 167.46343         1
## 113 24.78394 11.396552 255.67459         1
## 114 26.52156 13.686241 164.11634         0
## 115 26.50510 14.466270 248.37817         0
## 116 26.58018 12.328297 287.06294         1
## 117 25.92608 14.771279 217.66557         0
## 118 23.91203 14.736284 203.74326         1
## 119 27.30881 15.692218 243.16856         1
## 120 25.32708 13.009857 245.63389         0
## 121 25.93535 13.919335 259.13232         0
## 122 25.69387 15.409537 203.02439         1
## 123 24.20011 12.501555 235.06158         0
## 124 24.53324 13.622219 176.97483         1
## 125 25.11006 16.083741 278.69155         0
## 126 24.76475 13.926034 192.74710         1
## 127 25.86012 11.762551 204.05257         1
## 128 25.64865 16.255880 252.98314         0
## 129 24.48993 11.904071 140.25345         1
## 130 25.60849 17.183602 243.88590         1
## 131 26.86094 15.986996 213.92534         0
## 132 26.82818 14.455903 249.83580         1
## 133 22.41061 14.406908 160.10030         0
## 134 24.97480 11.484249 221.29088         0
## 135 25.02036 12.045565 228.43541         0
## 136 24.34246 13.990054 111.56698         0
## 137 22.56199 15.103281 128.55934         0
## 138 25.55650 15.948808 199.48294         0
## 139 26.76196 12.272406 238.29941         1
## 140 25.53661 14.268184 153.63230         1
## 141 28.25597 16.767946 253.44926         1
## 142 25.32184 14.766937 203.52560         1
## 143 27.60008 16.645640 210.19184         0
## 144 24.89702 15.773860 228.02142         1
## 145 26.20008 14.881262 222.41113         1
## 146 25.72992 15.244086 236.81337         1
## 147 26.76597 15.208943 204.74280         0
## 148 24.14640 12.789893 194.66957         0
## 149 26.73724 11.575259 226.18036         1
## 150 27.28330 15.767085 239.29117         0
## 151 24.67972 14.474395 202.96817         0
## 152 22.53689 12.546518 143.85339         1
## 153 25.11921 12.504372 101.85439         0
## 154 26.78078 12.685703 249.02604         1
## 155 26.11958 15.381618 250.49868         1
## 156 25.44986 13.384771 255.67208         1
## 157 25.06219 12.789134 252.41474         1
## 158 25.38574 12.936513 242.07715         0
## 159 23.78043 12.710588 218.35665         0
## 160 23.77847 11.446031 240.71192         0
## 161 25.59067 13.527017 242.93569         0
## 162 23.22829 11.756239 160.19386         0
## 163 25.00157 13.608261 192.50645         1
## 164 25.79880 12.338509 235.90843         1
## 165 27.61478 11.499431 316.46745         1
## 166 26.42999 15.869675 203.98146         1
## 167 25.95979 12.156466 305.15574         0
## 168 25.93172 16.951214 188.51392         0
## 169 22.64081 12.298461 145.92915         1
## 170 24.06584 13.673305 200.29956         1
## 171 26.39119 12.575732 247.20315         1
## 172 26.56508 14.557156 181.06405         1
## 173 23.87161 14.036194 196.13570         0
## 174 25.51213 12.276328 251.49210         0
## 175 23.51617 12.825810 190.31435         0
## 176 25.99334 14.803585 283.70783         0
## 177 25.83120 13.681926 200.60504         1
## 178 24.31176 13.950827 189.35118         0
## 179 24.67250 15.957223 181.95621         1
## 180 26.85467 15.943388 198.07853         0
## 181 24.86072 11.442487 262.13010         0
## 182 25.49572 12.475005 168.03509         1
## 183 23.75268 14.472872 178.47896         0
## 184 25.63985 16.215947 234.04577         0
## 185 23.18311 13.672994 124.39698         0
## 186 23.78918 13.210036 213.63432         1
## 187 26.13886 14.658622 201.29242         1
## 188 27.63023 16.057820 240.17966         1
## 189 25.73085 15.511313 194.87021         0
## 190 24.99189 14.037830 108.92948         1
## 191 27.82760 15.322436 177.56409         1
## 192 25.92141 16.473820 162.74332         1
## 193 25.45049 13.525196 359.60610         1
## 194 24.14701 14.103105  75.67260         0
## 195 24.83723 13.495156 231.21584         1
## 196 25.19157 12.767027 224.98404         0
## 197 26.11193 14.675873 260.64648         1
## 198 27.22023 16.360704 173.19966         0
## 199 26.19999 14.147575 219.01978         1
## 200 24.19164 14.915761 167.57821         1
## 201 25.74684 14.964712 175.17961         1
## 202 26.23002 14.108191 156.71149         1
## 203 23.96584 11.226996 166.42427         1
## 204 24.34919 13.030399 148.33683         1
## 205 24.14415 14.924564 155.52941         0
## 206 24.35622 15.008366 212.04533         1
## 207 25.93899 15.157989 156.01390         0
## 208 24.10531 11.712199 252.42904         1
## 209 25.92668 13.407767 234.83792         1
## 210 26.85944 13.841591 236.83735         1
## 211 26.30344 14.637992 261.75685         0
## 212 24.74404 13.382381 151.77747         0
## 213 25.58466 14.523734 204.01978         0
## 214 25.66031 16.265610 239.29369         0
## 215 26.88507 15.720743 270.35970         0
## 216 24.30535 15.215938 174.84747         1
## 217 25.50294 13.213642 193.86321         1
## 218 25.89383 15.040717 170.71503         0
## 219 27.72298 15.826671 261.40167         0
## 220 24.21762 15.630176 248.47175         0
## 221 26.31956 11.683581 303.38230         1
## 222 25.47482 13.980130 244.92406         0
## 223 28.31978 15.406602 288.42855         0
## 224 27.27134 16.795459 296.73760         1
## 225 24.43755 13.311402 185.41862         0
## 226 25.99392 15.840863  82.93915         0
## 227 26.66466 15.834659 189.92007         0
## 228 26.60558 16.298772 256.60362         1
## 229 26.03034 14.044566 235.37528         1
## 230 26.45130 12.119829 255.28710         0
## 231 24.89654 14.909330 115.74684         1
## 232 25.25628 16.493961 196.74873         1
## 233 25.54951 12.620907 289.91509         0
## 234 25.04806 13.793293 180.83857         1
## 235 26.54082 13.969341 163.07598         0
## 236 24.96268 15.813879 228.03589         0
## 237 24.42648 12.817892 173.63636         0
## 238 25.43365 13.414084 248.52279         1
## 239 25.96657 13.778567 280.72052         1
## 240 25.96564 15.080950 229.98020         1
## 241 24.62435 13.569069 144.37158         1
## 242 25.53551 14.930803 218.92990         0
## 243 25.12232 14.622429 130.37516         1
## 244 25.18927 14.964768 167.11732         1
## 245 25.56304 16.660481 212.31213         1
## 246 26.23981 13.009058 162.94262         0
## 247 22.76051 13.519288 117.32131         0
## 248 29.86088 18.088389 352.41460         0
## 249 25.10015 15.410613 154.81660         0
## 250 24.90765 14.901637 182.19263         0
## 251 26.00313 16.813563 183.84139         0
## 252 25.92759 12.726901 185.52510         0
## 253 25.70381 14.415195 252.45534         0
## 254 22.15387 12.821463 176.14678         1
## 255 25.93758 15.050885 244.48781         1
## 256 26.99361 15.387134 351.85845         1
## 257 25.93393 12.239890 244.04435         1
## 258 26.56293 15.110042 196.22031         0
## 259 25.68157 13.191222 311.28527         0
## 260 26.51588 13.221801 258.49011         1
## 261 25.41013 12.696974 246.83677         0
## 262 26.25473 13.032862 268.48178         0
## 263 22.85157 14.534949 233.93294         1
## 264 26.54292 14.045858 219.55317         1
## 265 27.40052 16.841369 263.27061         0
## 266 26.11784 15.157801 294.65391         1
## 267 26.35655 14.456156 267.29144         0
## 268 26.17479 14.947748 270.35960         1
## 269 27.05987 12.308099 277.23651         1
## 270 26.94352 15.352702 229.88337         1
## 271 25.22548 13.683371 239.21822         0
## 272 25.68475 13.170039 286.42646         1
## 273 24.67040 16.278576 202.65816         0
## 274 24.01600 12.422475 196.99885         0
## 275 25.40775 15.216392 257.32268         0
## 276 26.82978 14.119655 284.70109         1
## 277 24.55246 15.323945 228.97534         0
## 278 26.81283 15.191981 265.82160         1
## 279 27.44180 14.751683 199.14576         1
## 280 25.39444 14.231553 186.27238         0
## 281 27.01123 12.425555 211.48454         1
## 282 26.57081 13.894849 253.68482         1
## 283 26.33283 15.117615 174.59683         1
## 284 22.12643 12.759133 165.04209         0
## 285 26.51809 16.292108 242.15247         1
## 286 25.78063 14.454023 254.42717         1
## 287 26.90760 17.221601 253.82281         1
## 288 23.97897 13.100317 214.25571         1
## 289 25.93289 12.907728 215.27375         0
## 290 24.54191 12.124150 261.70187         1
## 291 26.83797 14.339734 282.75082         0
## 292 24.73686 11.720193 210.56385         0
## 293 26.04323 15.834369 198.09425         0
## 294 25.15737 13.979256 266.46904         0
## 295 24.94434 13.783219 181.19847         1
## 296 26.04133 13.704854 268.85551         0
## 297 25.24225 10.717594 203.46801         1
## 298 25.38229 14.869329 232.29966         0
## 299 24.56544 13.212993 198.78874         1
## 300 25.66030 14.738198 235.41881         0
## 301 24.36059 11.724288 223.91623         0
## 302 24.03015 12.345257 173.94383         1
## 303 25.13186 14.990965 155.68990         1
## 304 25.93316 14.800833 213.43632         0
## 305 27.05718 14.530854 217.37239         0
## 306 25.82012 16.727797 208.47023         1
## 307 26.34381 15.593293 194.70379         1
## 308 25.26551 14.912010 121.66709         1
## 309 24.83422 12.735285 223.47163         1
## 310 25.91513 12.836457 266.68391         0
## 311 26.74743 14.116508 220.41772         0
## 312 26.58914 16.329046 259.71003         1
## 313 23.08084 12.253216 149.89079         1
## 314 26.03574 13.818809 218.74804         0
## 315 27.04156 13.646096 289.89966         1
## 316 25.47795 14.501326 281.85143         0
## 317 24.73271 15.159820 182.29582         0
## 318 24.79988 12.477576 195.10925         0
## 319 24.96179 12.816969 150.47421         0
## 320 24.13306 13.676732 206.40085         1
## 321 26.07924 15.492798 181.25564         0
## 322 25.04205 14.072429 256.78141         0
## 323 25.43241 16.363370 192.19731         1
## 324 27.35508 14.023948 200.14659         1
## 325 23.72201 13.581518 193.21460         0
## 326 26.73142 16.246361 246.61698         0
## 327 24.13992 13.593257 179.20043         1
## 328 25.97739 15.293733 188.88778         0
## 329 24.46091 15.077944 165.38873         1
## 330 25.84589 13.403694 210.09449         0
## 331 24.73122 11.058352 182.60410         0
## 332 26.42164 14.398907 310.91963         0
## 333 25.24254 16.438288 180.17038         0
## 334 25.21008 12.408661 247.64455         0
## 335 26.09147 14.774645 235.98040         1
## 336 24.50504 12.659796 203.00214         0
## 337 28.34227 16.281879 302.12509         0
## 338 28.93940 13.983980 282.35611         1
## 339 26.18734 14.059229 196.93202         0
## 340 24.76878 13.717662 224.63339         1
## 341 26.55980 14.230946 271.35946         1
## 342 23.83952 14.348799 167.59591         0
## 343 23.93490 13.802864 169.04428         0
## 344 23.87446 13.898536 253.85102         0
## 345 25.40340 15.238771 191.08649         0
## 346 23.79106 14.035567 268.48205         0
## 347 24.09937 11.864872 183.67366         1
## 348 26.10779 12.457353 208.05381         1
## 349 26.38500 15.945596 148.12490         1
## 350 27.57700 13.239473 289.01373         1
## 351 25.86783 14.996200 203.16640         1
## 352 23.77355 13.301877 163.12826         1
## 353 25.70310 15.234876 178.84257         0
## 354 26.59248 13.609968 233.19229         1
## 355 25.52574 12.884740 197.16438         1
## 356 27.97448 13.944374 323.73028         1
## 357 25.41802 12.910867 259.35862         1
## 358 26.24686 15.259501 198.72064         1
## 359 27.40985 15.243038 192.28047         1
## 360 26.94480 13.526091 295.81484         0
## 361 24.77131 15.930568 216.86108         0
## 362 25.07896 13.916636 221.78901         1
## 363 24.84648 14.341369 125.29256         1
## 364 25.23240 14.746510 162.02674         0
## 365 25.82350 16.422657 164.15514         1
## 366 26.11146 16.389157 132.70363         1
## 367 25.55314 14.327853 234.51463         1
## 368 25.36341 13.504180 238.49637         0
## 369 24.82967 15.311605 157.96821         0
## 370 26.34754 15.630227 285.27916         0
## 371 25.88939 14.088970 250.92162         0
## 372 23.02664 12.470567 199.68270         0
## 373 24.24550 15.008110 211.80824         0
## 374 26.01238 15.263396 222.97139         1
## 375 24.48186 13.410294 194.24229         1
## 376 22.60694 12.831736 171.66831         0
## 377 24.65339 14.900188 192.18583         1
## 378 24.94628 13.304079 155.69899         0
## 379 23.94492 14.256087 186.69829         1
## 380 26.65404 16.918978 195.72015         0
## 381 24.98886 13.970706 172.71537         0
## 382 25.06223 15.846472 245.46485         1
## 383 23.53906 13.317658 199.39306         0
## 384 26.24936 14.746729 190.26525         0
## 385 25.63469 17.118225 228.11962         1
## 386 25.76029 13.485233 229.29481         1
## 387 24.65846 12.983904 156.81576         0
## 388 23.87136 15.221076 136.21324         0
## 389 25.80994 14.891737 270.76247         0
## 390 26.69780 13.809687 266.91755         0
## 391 23.80069 12.877204 206.70272         0
## 392 28.25263 13.460060 266.08603         1
## 393 25.34532 15.573503 150.37418         1
## 394 25.73986 18.440668 295.85160         0
## 395 26.55562 17.196329 197.13139         0
## 396 24.48463 16.167577 188.85519         0
## 397 23.40254 14.140809 196.12791         1
## 398 24.59377 11.435498 160.44293         1
## 399 25.53650 14.038478 205.69181         1
## 400 25.99610 12.832514 252.98641         1
## 401 28.27090 15.638011 320.02304         0
## 402 24.97730 11.173927 146.66335         0
## 403 26.44080 14.813621 206.27442         1
## 404 27.01760 16.000808 286.39534         1
## 405 27.13315 16.979809 257.99337         1
## 406 25.21919 14.849947 185.84047         0
## 407 25.68120 16.049116 176.91991         1
## 408 26.53463 15.171927 172.89935         1
## 409 23.17480 15.134177  96.47925         0
## 410 26.71146 12.886868 241.54404         1
## 411 25.21165 15.290789 196.45134         1
## 412 23.42672 14.064740 199.61672         1
## 413 27.03965 12.502802 255.39112         1
## 414 24.93401 14.117664 206.62849         0
## 415 24.08589 14.717988 202.11920         0
## 416 24.88508 13.022936 222.46028         0
## 417 27.14197 12.990597 323.81994         0
## 418 23.72648 13.907153 116.20418         1
## 419 29.63747 16.413280 323.04280         1
## 420 22.88077 14.369231 149.28867         0
## 421 26.00685 13.980665 297.60635         0
## 422 25.15046 13.403918 241.87404         0
## 423 27.75636 17.486841 212.09493         1
## 424 25.61391 12.871487 201.13537         0
## 425 26.42952 16.192530 256.04603         0
## 426 28.69186 14.847315 353.35551         0
## 427 25.73878 14.632544 192.32414         0
## 428 26.39262 14.788566 210.29290         1
## 429 24.37401 15.216084 158.24953         0
## 430 24.76091 12.813423 244.69507         1
## 431 26.12852 15.809807 181.29375         0
## 432 28.46161 13.974885 255.61380         0
## 433 23.81119 13.159490 135.39723         0
## 434 25.71240 17.343734 168.52761         1
## 435 23.04653 13.056301 127.15398         1
## 436 25.27853 18.073208 211.94218         0
## 437 24.14865 13.599923 183.94507         0
## 438 26.99562 14.533471 273.09744         1
## 439 28.04716 12.263789 360.78317         1
## 440 27.99184 14.075046 276.29293         1
## 441 29.00691 13.958972 308.69204         1
## 442 25.75267 13.793811 228.62744         1
## 443 26.65006 13.670639 226.30445         0
## 444 24.48259 12.297738 264.48682         0
## 445 25.19378 14.941285 140.79729         1
## 446 30.02507 17.239820 276.70594         1
## 447 26.30879 14.343898 224.16676         1
## 448 24.15242 13.124177 270.61060         0
## 449 26.06933 15.109535 198.78020         0
## 450 25.87033 14.647418 218.49527         1
## 451 25.36814 12.338186 219.91483         0
## 452 24.82058 17.593841 296.81038         0
## 453 25.74990 13.716944 267.65775         0
## 454 23.53976 14.654663 144.62806         0
## 455 24.20400 11.509538 243.21404         1
## 456 23.87439 16.416819 186.36149         0
## 457 24.17707 13.646999 142.12470         0
## 458 25.93571 14.539506 254.12972         0
## 459 23.42563  9.187151 257.31706         0
## 460 28.37411 12.871247 293.44313         1
## 461 26.34841 12.393671 258.51469         1
## 462 25.12550 13.949890 138.15223         1
## 463 27.34674 15.245191 278.51344         0
## 464 23.71500  9.787497 257.71266         1
## 465 26.95586 13.707716 276.02723         0
## 466 25.10601 17.664682 155.80390         0
## 467 25.02304 14.953277 137.11964         1
## 468 23.57642 15.937047 211.19923         1
## 469 24.67981 15.377539 272.68266         0
## 470 27.25027 15.198491 272.66829         0
## 471 25.40680 15.323189 165.65537         1
## 472 27.54866 14.102680 207.60452         1
## 473 26.09885 14.269052 242.74766         1
## 474 26.38810 15.349087 183.71818         1
## 475 26.18298 14.434507 219.87283         1
## 476 25.04984 13.585372 211.25322         0
## 477 27.58656 13.327393 269.49535         0
## 478 22.32275 11.387459 102.50166         0
## 479 25.53536 14.323856 194.58279         0
## 480 24.88320 13.791699 256.28019         1
## 481 26.34697 14.945574 206.74715         0
## 482 27.86171 20.047787 296.71898         0
## 483 25.78034 14.376338 254.95875         0
## 484 26.83404 16.343233 236.92803         0
## 485 26.43990 13.483230 277.53363         1
## 486 25.88610 14.249398 222.81560         0
## 487 26.57274 12.448554 282.73317         1
## 488 25.66364 17.499347 231.79560         1
## 489 23.73441 13.103178 157.65195         1
## 490 24.84372 13.625779 143.36230         1
## 491 26.80982 15.642331 279.58677         1
## 492 24.34680 13.797708 207.50747         0
## 493 27.44017 13.710044 302.07536         0
## 494 27.28606 16.708241 284.82170         1
## 495 26.53782 14.796240 255.09687         1
## 496 24.88231 13.895077 149.66834         0
## 497 25.61486 13.568672 138.87396         1
## 498 25.80763 15.764450 236.49060         1
## 499 26.70328 13.156287 224.69851         1
## 500 25.61063 16.477538 260.91417         0
# b
count_tr <- 0
for (i in 1:1000) {
  set.seed(2022)
  df <- simulate_data(n=100)
  fit <- lm(bmi~hgb+chol+treatment, data=df)
  s <- summary(fit)
  pvalue <- s$coefficients[4,4]
  if(pvalue < 0.05){
    count_tr <- count_tr + 1
  }
}
count_tr
## [1] 0
## c
sample_sizes <- seq(40,200,by=20)
proportions <- c()
for (s in 1:length(sample_sizes)) {
  nn <- sample_sizes[s]
  count_tr <- 0
  for (i in 1:1000) {
    set.seed(2022)
    df <- simulate_data(n=nn)
    fit <- lm(bmi~hgb+chol+treatment, data=df)
    mysummary <- summary(fit)
    pvalue <- mysummary$coefficients[4,4]
    if(pvalue < 0.05){
      count_tr <- count_tr + 1
    }
  }
  proportions[s] <- count_tr/1000
}
proportions
## [1] 0 0 1 0 1 1 1 1 1
mean(proportions) # mean of power
## [1] 0.6666667
#--------------------------
# 2
#-------------------------
# 2 - a
plot(sample_sizes, proportions, col='blue', lwd=5)
abline(h=0.80, col='red', lty=3, lwd=3)

# 2- b
# minimum sample size that has power > 0.80
sample_sizes[which(proportions >= 0.80)[1]]
## [1] 80
# 2 - c
xa = seq(min(sample_sizes), max(sample_sizes), by=40)
ya = seq(min(proportions), max(proportions), length=9)
plot(sample_sizes, proportions, col='blue', lwd=5, xaxt='n', yaxt='n', ylab='Power', xlab='Sample Sizes')
abline(h=0.80, col='red', lty=3, lwd=3)
axis(side=1, at=xa, labels = seq(40,200,by=40))
axis(side=2, at=ya, labels = paste0(seq(0.2,1,by=0.1)*100,'%'))

follow me on RPubs for more!

www.rstudio.ir