Advertisement
Guest User

BIO360 Lab 3 Code

a guest
Feb 19th, 2020
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.02 KB | None | 0 0
  1. #####################
  2. # BIO360 Lab 3 Code #
  3. #####################
  4.  
  5. # Required R packages
  6. #####################
  7.  
  8. library(Rcmdr)
  9. library(PASWR)
  10.  
  11. # Import data
  12. #############
  13.  
  14. # Import dataset:
  15. # ---------------
  16. SelfEfficacy <-
  17. read.table("/Users/riya/OneDrive - University of Toronto/BIO360 Labs/BIO360-Lab3/SelfEfficacy.txt",
  18. header=TRUE, sep="\t", na.strings="NA", dec=".", strip.white=TRUE)
  19.  
  20. #Reorder groups:
  21. # --------------
  22. SelfEfficacy$Group <- with(SelfEfficacy, factor(Group, levels=c('Intervention','Control')))
  23.  
  24. ##########################
  25. # Q1. Short Term Effects #
  26. ##########################
  27.  
  28. # The goal of this section is to test whether patients who receive a tailored information package have higher initial self-efficacy than patients who don't receive hte package, indicate the short-term effect of informatino packages at increasing self-efficacy in stroke patients.
  29. # For each type of group (Intervention, Control), a one-sided Wilcoxon rank sum test is performed to test whether the median of outcome score is significantly higher than 0.
  30.  
  31. # Calculate summary statistics for hypothesis 1:
  32. # ----------------------------------------------
  33. numSummary(SelfEfficacy[,c("Initial.SE1", "Initial.SE2", "Initial.SE3",
  34. "Initial.SE4", "Initial.SE5", "Initial.SE6", "Initial.SE7", "Initial.SE8",
  35. "Initial.SE9"), drop=FALSE], groups=SelfEfficacy$Group, statistics=c("mean",
  36. "sd", "IQR", "quantiles"), quantiles=c(0.5))
  37. # Two-sample t-tests (don't interpret)
  38. # ------------------------------------
  39. t.test(Initial.SE1~Group, alternative='greater', conf.level=.95, var.equal=FALSE, data=SelfEfficacy)
  40. t.test(Initial.SE2~Group, alternative='greater', conf.level=.95, var.equal=FALSE, data=SelfEfficacy)
  41. t.test(Initial.SE3~Group, alternative='greater', conf.level=.95, var.equal=FALSE, data=SelfEfficacy)
  42. t.test(Initial.SE4~Group, alternative='greater', conf.level=.95, var.equal=FALSE, data=SelfEfficacy)
  43. t.test(Initial.SE5~Group, alternative='greater', conf.level=.95, var.equal=FALSE, data=SelfEfficacy)
  44. t.test(Initial.SE6~Group, alternative='greater', conf.level=.95, var.equal=FALSE, data=SelfEfficacy)
  45. t.test(Initial.SE7~Group, alternative='greater', conf.level=.95, var.equal=FALSE, data=SelfEfficacy)
  46. t.test(Initial.SE8~Group, alternative='greater', conf.level=.95, var.equal=FALSE, data=SelfEfficacy)
  47. t.test(Initial.SE9~Group, alternative='greater', conf.level=.95, var.equal=FALSE, data=SelfEfficacy)
  48.  
  49.  
  50. # Wilcoxon rank sum tests (use this)
  51. # ----------------------------------
  52. with(SelfEfficacy, tapply(Initial.SE1, Group, median, na.rm=TRUE))
  53. wilcox.test(Initial.SE1 ~ Group, alternative="greater", data=SelfEfficacy)
  54.  
  55. with(SelfEfficacy, tapply(Initial.SE2, Group, median, na.rm=TRUE))
  56. wilcox.test(Initial.SE2 ~ Group, alternative="greater", data=SelfEfficacy)
  57.  
  58. with(SelfEfficacy, tapply(Initial.SE3, Group, median, na.rm=TRUE))
  59. wilcox.test(Initial.SE3 ~ Group, alternative="greater", data=SelfEfficacy)
  60.  
  61. with(SelfEfficacy, tapply(Initial.SE4, Group, median, na.rm=TRUE))
  62. wilcox.test(Initial.SE4 ~ Group, alternative="greater", data=SelfEfficacy)
  63.  
  64. with(SelfEfficacy, tapply(Initial.SE5, Group, median, na.rm=TRUE))
  65. wilcox.test(Initial.SE5 ~ Group, alternative="greater", data=SelfEfficacy)
  66.  
  67. with(SelfEfficacy, tapply(Initial.SE6, Group, median, na.rm=TRUE))
  68. wilcox.test(Initial.SE6 ~ Group, alternative="greater", data=SelfEfficacy)
  69.  
  70. with(SelfEfficacy, tapply(Initial.SE7, Group, median, na.rm=TRUE))
  71. wilcox.test(Initial.SE7 ~ Group, alternative="greater", data=SelfEfficacy)
  72.  
  73. with(SelfEfficacy, tapply(Initial.SE8, Group, median, na.rm=TRUE))
  74. wilcox.test(Initial.SE8 ~ Group, alternative="greater", data=SelfEfficacy)
  75.  
  76. with(SelfEfficacy, tapply(Initial.SE9, Group, median, na.rm=TRUE))
  77. wilcox.test(Initial.SE9 ~ Group, alternative="greater", data=SelfEfficacy)
  78.  
  79. #########################
  80. # Q2. Long Term Effects #
  81. #########################
  82.  
  83. # The goal of this section is to see if self-efficacy of patients who receive a tailored information package increases over time.
  84. # For the Intervention group, a sign test is performed to test whether the median of outcome score is significantly higher than 0.
  85.  
  86. # Subset data:
  87. # ------------
  88. InterventionGroup <- subset(SelfEfficacy, subset=Group == "Intervention", select=c(Follow.up.SE1,Follow.up.SE2,Follow.up.SE3,
  89. Follow.up.SE4,Follow.up.SE5,Follow.up.SE6,Follow.up.SE7,Follow.up.SE8,Follow.up.SE9,Group,Initial.SE1,Initial.SE2,Initial.SE3,
  90. Initial.SE4,Initial.SE5,Initial.SE6,Initial.SE7,Initial.SE8,Initial.SE9))
  91. # Create 'differences' variables:
  92. # -------------------------------
  93. InterventionGroup$Difference.SE1 <- with(InterventionGroup, Follow.up.SE1- Initial.SE1)
  94. InterventionGroup$Difference.SE2 <- with(InterventionGroup, Follow.up.SE2- Initial.SE2)
  95. InterventionGroup$Difference.SE3 <- with(InterventionGroup, Follow.up.SE3- Initial.SE3)
  96. InterventionGroup$Difference.SE4 <- with(InterventionGroup, Follow.up.SE4- Initial.SE4)
  97. InterventionGroup$Difference.SE5 <- with(InterventionGroup, Follow.up.SE5- Initial.SE5)
  98. InterventionGroup$Difference.SE6 <- with(InterventionGroup, Follow.up.SE6- Initial.SE6)
  99. InterventionGroup$Difference.SE7 <- with(InterventionGroup, Follow.up.SE7- Initial.SE7)
  100. InterventionGroup$Difference.SE8 <- with(InterventionGroup, Follow.up.SE8- Initial.SE8)
  101. InterventionGroup$Difference.SE9 <- with(InterventionGroup, Follow.up.SE9- Initial.SE9)
  102.  
  103.  
  104. # Calculate summary statistics for hypothesis 2:
  105. # ----------------------------------------------
  106. numSummary(InterventionGroup[,c("Difference.SE1", "Difference.SE2", "Difference.SE3",
  107. "Difference.SE4", "Difference.SE5", "Difference.SE6", "Difference.SE7", "Difference.SE8",
  108. "Difference.SE9"), drop=FALSE], groups=InterventionGroup$Group, statistics=c("mean",
  109. "sd", "IQR", "quantiles"), quantiles=c(0.5))
  110.  
  111. # Paired t-tests (one-sample t-test on differences)
  112. # -------------------------------------------------
  113. with(InterventionGroup, (t.test(Difference.SE1, alternative='greater', mu=0.0, conf.level=.95)))
  114. with(InterventionGroup, (t.test(Difference.SE2, alternative='greater', mu=0.0, conf.level=.95)))
  115. with(InterventionGroup, (t.test(Difference.SE3, alternative='greater', mu=0.0, conf.level=.95)))
  116. with(InterventionGroup, (t.test(Difference.SE4, alternative='greater', mu=0.0, conf.level=.95)))
  117. with(InterventionGroup, (t.test(Difference.SE5, alternative='greater', mu=0.0, conf.level=.95)))
  118. with(InterventionGroup, (t.test(Difference.SE6, alternative='greater', mu=0.0, conf.level=.95)))
  119. with(InterventionGroup, (t.test(Difference.SE7, alternative='greater', mu=0.0, conf.level=.95)))
  120. with(InterventionGroup, (t.test(Difference.SE8, alternative='greater', mu=0.0, conf.level=.95)))
  121. with(InterventionGroup, (t.test(Difference.SE9, alternative='greater', mu=0.0, conf.level=.95)))
  122.  
  123.  
  124. # Sign tests (interpret this)
  125. # ---------------------------
  126. with(InterventionGroup, (SIGN.test(Follow.up.SE1, Initial.SE1, alternative = "greater")))
  127. with(InterventionGroup, (SIGN.test(Follow.up.SE2, Initial.SE2, alternative = "greater")))
  128. with(InterventionGroup, (SIGN.test(Follow.up.SE3, Initial.SE3, alternative = "greater")))
  129. with(InterventionGroup, (SIGN.test(Follow.up.SE4, Initial.SE4, alternative = "greater")))
  130. with(InterventionGroup, (SIGN.test(Follow.up.SE5, Initial.SE5, alternative = "greater")))
  131. with(InterventionGroup, (SIGN.test(Follow.up.SE6, Initial.SE6, alternative = "greater")))
  132. with(InterventionGroup, (SIGN.test(Follow.up.SE7, Initial.SE7, alternative = "greater")))
  133. with(InterventionGroup, (SIGN.test(Follow.up.SE8, Initial.SE8, alternative = "greater")))
  134. with(InterventionGroup, (SIGN.test(Follow.up.SE9, Initial.SE9, alternative = "greater")))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement