library(ggplot2)
data("ChickWeight")

ggplot(ChickWeight, aes(x = weight)) + 
  geom_histogram(binwidth = 50, fill = "blue", color = "black") +
  labs(title = "Distribution of Chick Weights", x = "Weight", y = "Frequency")

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(tidyr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
#long to wide
wide_data <- ChickWeight %>%
  spread(key = Time, value = weight)

head(wide_data)
##   Chick Diet  0  2  4  6  8 10 12 14 16  18  20  21
## 1    18    1 39 35 NA NA NA NA NA NA NA  NA  NA  NA
## 2    16    1 41 45 49 51 57 51 54 NA NA  NA  NA  NA
## 3    15    1 41 49 56 64 68 68 67 68 NA  NA  NA  NA
## 4    13    1 41 48 53 60 65 67 71 70 71  81  91  96
## 5     9    1 42 51 59 68 85 96 90 92 93 100 100  98
## 6    20    1 41 47 54 58 65 73 77 89 98 107 115 117
tail(wide_data)
##    Chick Diet  0  2  4  6   8  10  12  14  16  18  20  21
## 45    47    4 41 53 66 79 100 123 148 157 168 185 210 205
## 46    49    4 40 53 64 85 108 128 152 166 184 203 233 237
## 47    46    4 40 52 62 82 101 120 144 156 173 210 231 238
## 48    50    4 41 54 67 84 105 122 155 175 205 234 264 264
## 49    42    4 42 49 63 84 103 126 160 174 204 234 269 281
## 50    48    4 39 50 62 80 104 125 154 170 222 261 303 322

Including Plots

You can also embed plots, for example:

#wide to long
long_data <- wide_data %>%
  gather(key = "Time", value = "weight",-Chick,-Diet)

head(wide_data)
##   Chick Diet  0  2  4  6  8 10 12 14 16  18  20  21
## 1    18    1 39 35 NA NA NA NA NA NA NA  NA  NA  NA
## 2    16    1 41 45 49 51 57 51 54 NA NA  NA  NA  NA
## 3    15    1 41 49 56 64 68 68 67 68 NA  NA  NA  NA
## 4    13    1 41 48 53 60 65 67 71 70 71  81  91  96
## 5     9    1 42 51 59 68 85 96 90 92 93 100 100  98
## 6    20    1 41 47 54 58 65 73 77 89 98 107 115 117
tail(wide_data)
##    Chick Diet  0  2  4  6   8  10  12  14  16  18  20  21
## 45    47    4 41 53 66 79 100 123 148 157 168 185 210 205
## 46    49    4 40 53 64 85 108 128 152 166 184 203 233 237
## 47    46    4 40 52 62 82 101 120 144 156 173 210 231 238
## 48    50    4 41 54 67 84 105 122 155 175 205 234 264 264
## 49    42    4 42 49 63 84 103 126 160 174 204 234 269 281
## 50    48    4 39 50 62 80 104 125 154 170 222 261 303 322

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

shapiro_test_result <- shapiro.test(ChickWeight$weight)
shapiro_test_result
## 
##  Shapiro-Wilk normality test
## 
## data:  ChickWeight$weight
## W = 0.90866, p-value < 2.2e-16
format(shapiro_test_result$p.value, scientific = FALSE)
## [1] "0.000000000000000003873675"
#사료 1이랑 사료2랑의 weight 비교
diet_1_weight <- ChickWeight %>%
  filter(Diet == 1) %>%
  select(weight)

#사료 1이랑 사료2랑의 weight 비교
diet_2_weight <- ChickWeight %>%
  filter(Diet == 2) %>%
  select(weight)

wilcox_test_result <- wilcox.test(diet_1_weight$weight, diet_2_weight$weight)
wilcox_test_result
## 
##  Wilcoxon rank sum test with continuity correction
## 
## data:  diet_1_weight$weight and diet_2_weight$weight
## W = 11213, p-value = 0.02181
## alternative hypothesis: true location shift is not equal to 0
library(MASS)
## 
## Attaching package: 'MASS'
## The following object is masked from 'package:dplyr':
## 
##     select
data("Cars93")
summary(Cars93)
##     Manufacturer     Model         Type      Min.Price         Price      
##  Chevrolet: 8    100    : 1   Compact:16   Min.   : 6.70   Min.   : 7.40  
##  Ford     : 8    190E   : 1   Large  :11   1st Qu.:10.80   1st Qu.:12.20  
##  Dodge    : 6    240    : 1   Midsize:22   Median :14.70   Median :17.70  
##  Mazda    : 5    300E   : 1   Small  :21   Mean   :17.13   Mean   :19.51  
##  Pontiac  : 5    323    : 1   Sporty :14   3rd Qu.:20.30   3rd Qu.:23.30  
##  Buick    : 4    535i   : 1   Van    : 9   Max.   :45.40   Max.   :61.90  
##  (Other)  :57    (Other):87                                               
##    Max.Price       MPG.city      MPG.highway                  AirBags  
##  Min.   : 7.9   Min.   :15.00   Min.   :20.00   Driver & Passenger:16  
##  1st Qu.:14.7   1st Qu.:18.00   1st Qu.:26.00   Driver only       :43  
##  Median :19.6   Median :21.00   Median :28.00   None              :34  
##  Mean   :21.9   Mean   :22.37   Mean   :29.09                          
##  3rd Qu.:25.3   3rd Qu.:25.00   3rd Qu.:31.00                          
##  Max.   :80.0   Max.   :46.00   Max.   :50.00                          
##                                                                        
##  DriveTrain  Cylinders    EngineSize      Horsepower         RPM      
##  4WD  :10   3     : 3   Min.   :1.000   Min.   : 55.0   Min.   :3800  
##  Front:67   4     :49   1st Qu.:1.800   1st Qu.:103.0   1st Qu.:4800  
##  Rear :16   5     : 2   Median :2.400   Median :140.0   Median :5200  
##             6     :31   Mean   :2.668   Mean   :143.8   Mean   :5281  
##             8     : 7   3rd Qu.:3.300   3rd Qu.:170.0   3rd Qu.:5750  
##             rotary: 1   Max.   :5.700   Max.   :300.0   Max.   :6500  
##                                                                       
##   Rev.per.mile  Man.trans.avail Fuel.tank.capacity   Passengers   
##  Min.   :1320   No :32          Min.   : 9.20      Min.   :2.000  
##  1st Qu.:1985   Yes:61          1st Qu.:14.50      1st Qu.:4.000  
##  Median :2340                   Median :16.40      Median :5.000  
##  Mean   :2332                   Mean   :16.66      Mean   :5.086  
##  3rd Qu.:2565                   3rd Qu.:18.80      3rd Qu.:6.000  
##  Max.   :3755                   Max.   :27.00      Max.   :8.000  
##                                                                   
##      Length        Wheelbase         Width        Turn.circle   
##  Min.   :141.0   Min.   : 90.0   Min.   :60.00   Min.   :32.00  
##  1st Qu.:174.0   1st Qu.: 98.0   1st Qu.:67.00   1st Qu.:37.00  
##  Median :183.0   Median :103.0   Median :69.00   Median :39.00  
##  Mean   :183.2   Mean   :103.9   Mean   :69.38   Mean   :38.96  
##  3rd Qu.:192.0   3rd Qu.:110.0   3rd Qu.:72.00   3rd Qu.:41.00  
##  Max.   :219.0   Max.   :119.0   Max.   :78.00   Max.   :45.00  
##                                                                 
##  Rear.seat.room   Luggage.room       Weight         Origin              Make   
##  Min.   :19.00   Min.   : 6.00   Min.   :1695   USA    :48   Acura Integra: 1  
##  1st Qu.:26.00   1st Qu.:12.00   1st Qu.:2620   non-USA:45   Acura Legend : 1  
##  Median :27.50   Median :14.00   Median :3040                Audi 100     : 1  
##  Mean   :27.83   Mean   :13.89   Mean   :3073                Audi 90      : 1  
##  3rd Qu.:30.00   3rd Qu.:15.00   3rd Qu.:3525                BMW 535i     : 1  
##  Max.   :36.00   Max.   :22.00   Max.   :4105                Buick Century: 1  
##  NAs    :2       NAs    :11                                  (Other)      :87
#log transformation

Cars93$LogPrice <- log(Cars93$Price)
summary(Cars93$LogPrice)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   2.001   2.501   2.874   2.865   3.148   4.126
hist(Cars93$LogPrice, breaks = 20, main = "Log-Transformed Price")

hist(Cars93$Price, breaks = 20, main = "Original Transformed Price")

#Q-Q plot

qqnorm(Cars93$LogPrice)
qqline(Cars93$LogPrice, col = "red")

#이상치 추가방법
Cars93$Pricewithoutliers <- Cars93$Price
Cars93$Pricewithoutliers[c(1,2,3)] <- c(100,120,150)
qqnorm(Cars93$Pricewithoutliers)
qqline(Cars93$Pricewithoutliers, col = "blue")

#미국산 vs 비미국산
usa_cars <- Cars93$Pricewithoutliers[Cars93$Origin == "USA"]
non_usa_cars <- Cars93$Pricewithoutliers[Cars93$Origin != "USA"]

wilcox_test_result <- wilcox.test(usa_cars, non_usa_cars, exact = FALSE)
wilcox_test_result
## 
##  Wilcoxon rank sum test with continuity correction
## 
## data:  usa_cars and non_usa_cars
## W = 987.5, p-value = 0.4794
## alternative hypothesis: true location shift is not equal to 0
#there are no significant difference between usa cars and non usa cars because the p-value is higher than 0.05.
library(MASS)
library(ggplot2)
library(dplyr)
data("anorexia")
names(anorexia)
## [1] "Treat"  "Prewt"  "Postwt"
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
ft_data <- anorexia %>%
  filter(Treat == "FT")

#sekecting Prewt from the FT
Prewt_ft <- anorexia %>%
  filter(Treat == "FT") %>%
  dplyr::select(Prewt)

#sekecting postwt from the FT
Postwt_ft <- anorexia %>%
  filter(Treat == "FT") %>%
  dplyr::select(Postwt)
#extract the data
Prewt_ft <-ft_data$Prewt
Postwt_ft <-ft_data$Postwt
hist(Prewt_ft, main = "Weight Before Treatment (FT)", xlab = "Pre-treatment Weight", col = "lightblue")

hist(Postwt_ft , main = "Weight After Treatment (FT)", xlab = "Post-treatment Weight", col = "lightgreen")

shapiro_pre1 <- shapiro.test(Prewt_ft)
shapiro_pre1
## 
##  Shapiro-Wilk normality test
## 
## data:  Prewt_ft
## W = 0.98821, p-value = 0.9972
shapiro_pre1 <- shapiro.test(Postwt_ft)
shapiro_pre1
## 
##  Shapiro-Wilk normality test
## 
## data:  Postwt_ft
## W = 0.83928, p-value = 0.007391
#before weight is normal
#after weight is not normal
#so we use the wilconxon signed-rank test 


wilcox_test_result <- wilcox.test(Prewt_ft, Postwt_ft, paired = TRUE, exact = FALSE)
print(wilcox_test_result)
## 
##  Wilcoxon signed rank test with continuity correction
## 
## data:  Prewt_ft and Postwt_ft
## V = 11, p-value = 0.002091
## alternative hypothesis: true location shift is not equal to 0
#There are significant differences between preweight and postweight as the p-value is 0.002091.