R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

library(MASS)
data(anorexia)
anorexia
##    Treat Prewt Postwt
## 1   Cont  80.7   80.2
## 2   Cont  89.4   80.1
## 3   Cont  91.8   86.4
## 4   Cont  74.0   86.3
## 5   Cont  78.1   76.1
## 6   Cont  88.3   78.1
## 7   Cont  87.3   75.1
## 8   Cont  75.1   86.7
## 9   Cont  80.6   73.5
## 10  Cont  78.4   84.6
## 11  Cont  77.6   77.4
## 12  Cont  88.7   79.5
## 13  Cont  81.3   89.6
## 14  Cont  78.1   81.4
## 15  Cont  70.5   81.8
## 16  Cont  77.3   77.3
## 17  Cont  85.2   84.2
## 18  Cont  86.0   75.4
## 19  Cont  84.1   79.5
## 20  Cont  79.7   73.0
## 21  Cont  85.5   88.3
## 22  Cont  84.4   84.7
## 23  Cont  79.6   81.4
## 24  Cont  77.5   81.2
## 25  Cont  72.3   88.2
## 26  Cont  89.0   78.8
## 27   CBT  80.5   82.2
## 28   CBT  84.9   85.6
## 29   CBT  81.5   81.4
## 30   CBT  82.6   81.9
## 31   CBT  79.9   76.4
## 32   CBT  88.7  103.6
## 33   CBT  94.9   98.4
## 34   CBT  76.3   93.4
## 35   CBT  81.0   73.4
## 36   CBT  80.5   82.1
## 37   CBT  85.0   96.7
## 38   CBT  89.2   95.3
## 39   CBT  81.3   82.4
## 40   CBT  76.5   72.5
## 41   CBT  70.0   90.9
## 42   CBT  80.4   71.3
## 43   CBT  83.3   85.4
## 44   CBT  83.0   81.6
## 45   CBT  87.7   89.1
## 46   CBT  84.2   83.9
## 47   CBT  86.4   82.7
## 48   CBT  76.5   75.7
## 49   CBT  80.2   82.6
## 50   CBT  87.8  100.4
## 51   CBT  83.3   85.2
## 52   CBT  79.7   83.6
## 53   CBT  84.5   84.6
## 54   CBT  80.8   96.2
## 55   CBT  87.4   86.7
## 56    FT  83.8   95.2
## 57    FT  83.3   94.3
## 58    FT  86.0   91.5
## 59    FT  82.5   91.9
## 60    FT  86.7  100.3
## 61    FT  79.6   76.7
## 62    FT  76.9   76.8
## 63    FT  94.2  101.6
## 64    FT  73.4   94.9
## 65    FT  80.5   75.2
## 66    FT  81.6   77.8
## 67    FT  82.1   95.5
## 68    FT  77.6   90.7
## 69    FT  83.5   92.5
## 70    FT  89.9   93.8
## 71    FT  86.0   91.7
## 72    FT  87.3   98.0
str(anorexia)
## 'data.frame':    72 obs. of  3 variables:
##  $ Treat : Factor w/ 3 levels "CBT","Cont","FT": 2 2 2 2 2 2 2 2 2 2 ...
##  $ Prewt : num  80.7 89.4 91.8 74 78.1 88.3 87.3 75.1 80.6 78.4 ...
##  $ Postwt: num  80.2 80.1 86.4 86.3 76.1 78.1 75.1 86.7 73.5 84.6 ...
summary(anorexia)
##   Treat        Prewt           Postwt      
##  CBT :29   Min.   :70.00   Min.   : 71.30  
##  Cont:26   1st Qu.:79.60   1st Qu.: 79.33  
##  FT  :17   Median :82.30   Median : 84.05  
##            Mean   :82.41   Mean   : 85.17  
##            3rd Qu.:86.00   3rd Qu.: 91.55  
##            Max.   :94.90   Max.   :103.60
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ✖ dplyr::select() masks MASS::select()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
data <- anorexia %>%
  filter(Treat == "FT")
data
##    Treat Prewt Postwt
## 1     FT  83.8   95.2
## 2     FT  83.3   94.3
## 3     FT  86.0   91.5
## 4     FT  82.5   91.9
## 5     FT  86.7  100.3
## 6     FT  79.6   76.7
## 7     FT  76.9   76.8
## 8     FT  94.2  101.6
## 9     FT  73.4   94.9
## 10    FT  80.5   75.2
## 11    FT  81.6   77.8
## 12    FT  82.1   95.5
## 13    FT  77.6   90.7
## 14    FT  83.5   92.5
## 15    FT  89.9   93.8
## 16    FT  86.0   91.7
## 17    FT  87.3   98.0
data$weight_change <- data$Postwt-data$Prewt
data
##    Treat Prewt Postwt weight_change
## 1     FT  83.8   95.2          11.4
## 2     FT  83.3   94.3          11.0
## 3     FT  86.0   91.5           5.5
## 4     FT  82.5   91.9           9.4
## 5     FT  86.7  100.3          13.6
## 6     FT  79.6   76.7          -2.9
## 7     FT  76.9   76.8          -0.1
## 8     FT  94.2  101.6           7.4
## 9     FT  73.4   94.9          21.5
## 10    FT  80.5   75.2          -5.3
## 11    FT  81.6   77.8          -3.8
## 12    FT  82.1   95.5          13.4
## 13    FT  77.6   90.7          13.1
## 14    FT  83.5   92.5           9.0
## 15    FT  89.9   93.8           3.9
## 16    FT  86.0   91.7           5.7
## 17    FT  87.3   98.0          10.7
hist(data$weight_change, breaks=20, main= "Change of weight after family treatment")

shapiro_test_result <- shapiro.test(data$Prewt)
shapiro_test_result
## 
##  Shapiro-Wilk normality test
## 
## data:  data$Prewt
## W = 0.98821, p-value = 0.9972
shapiro_test_result<-shapiro.test(data$Postwt)
shapiro_test_result
## 
##  Shapiro-Wilk normality test
## 
## data:  data$Postwt
## W = 0.83928, p-value = 0.007391
# Since the p value of the data after treatment is less than 0.05, it does not follow a normal distribution and the Prewt and Postwt data are dependent samples because they come from the same group. Additionally, since the histogram for weight change does not appear to have a normal distribution, the median values must be compared using the Wilcoxon Signed Rank Test.
wilcox_test_result <- wilcox.test(data$Prewt, data$Postwt, paired=TRUE, exact=FALSE)
wilcox_test_result
## 
##  Wilcoxon signed rank test with continuity correction
## 
## data:  data$Prewt and data$Postwt
## V = 11, p-value = 0.002091
## alternative hypothesis: true location shift is not equal to 0
# Since the p-value is less than 0.05, there is a difference in the median value, and FT treatment is effective.

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.