User Tools

Site Tools


r:general_statistics

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
r:general_statistics [2016/11/02 10:01] – [e.g.,] hkimscilr:general_statistics [2019/10/11 07:53] – [Forming a Confidence Interval for a Mean] hkimscil
Line 53: Line 53:
  
 <code>suburbs <- read.csv("suburbs.csv", head=T, sep=" ") <code>suburbs <- read.csv("suburbs.csv", head=T, sep=" ")
 +suburbs <- read.csv("http://commres.net/wiki/_export/code/r/general_statistics?codeblock=1", head=T, sep="\t")
 </code> </code>
  
Line 76: Line 77:
 ====== Calculating Relative Frequencies ====== ====== Calculating Relative Frequencies ======
 <code> <code>
-> mean(Cars93$MPG.city > 14)  # see the summary(Cars93$MPG.city) the above+> mean(Cars93$MPG.city > 14)  # see the summary(Cars93$MPG.city) the above = 100%, min = 15이므로
 [1] 1 [1] 1
  
Line 180: Line 181:
  
 </code> </code>
 +<code>> library(MASS)
 +> cardata <- data.frame(Cars93$Origin, Cars93$Type)
 +> cardata
 +   Cars93.Origin Cars93.Type
 +1        non-USA       Small
 +2        non-USA     Midsize
 +3        non-USA     Compact
 +4        non-USA     Midsize
 +5        non-USA     Midsize
 +6            USA     Midsize
 +7            USA       Large
 +8            USA       Large
 +9            USA     Midsize
 +10           USA       Large
 +11           USA     Midsize
 +12           USA     Compact
 +13           USA     Compact
 +14           USA      Sporty
 +15           USA     Midsize
 +16           USA         Van
 +17           USA         Van
 +18           USA       Large
 +19           USA      Sporty
 +20           USA       Large
 +21           USA     Compact
 +22           USA       Large
 +23           USA       Small
 +24           USA       Small
 +25           USA     Compact
 +26           USA         Van
 +27           USA     Midsize
 +28           USA      Sporty
 +29           USA       Small
 +30           USA       Large
 +31           USA       Small
 +32           USA       Small
 +33           USA     Compact
 +34           USA      Sporty
 +35           USA      Sporty
 +36           USA         Van
 +37           USA     Midsize
 +38           USA       Large
 +39       non-USA       Small
 +40       non-USA      Sporty
 +41       non-USA      Sporty
 +42       non-USA       Small
 +43       non-USA     Compact
 +44       non-USA       Small
 +45       non-USA       Small
 +46       non-USA      Sporty
 +47       non-USA     Midsize
 +48       non-USA     Midsize
 +49       non-USA     Midsize
 +50       non-USA     Midsize
 +51           USA     Midsize
 +52           USA       Large
 +53       non-USA       Small
 +54       non-USA       Small
 +55       non-USA     Compact
 +56       non-USA         Van
 +57       non-USA      Sporty
 +58       non-USA     Compact
 +59       non-USA     Midsize
 +60           USA      Sporty
 +61           USA     Midsize
 +62       non-USA       Small
 +63       non-USA     Midsize
 +64       non-USA       Small
 +65       non-USA     Compact
 +66       non-USA         Van
 +67       non-USA     Midsize
 +68           USA     Compact
 +69           USA     Midsize
 +70           USA         Van
 +71           USA       Large
 +72           USA      Sporty
 +73           USA       Small
 +74           USA     Compact
 +75           USA      Sporty
 +76           USA     Midsize
 +77           USA       Large
 +78       non-USA     Compact
 +79           USA       Small
 +80       non-USA       Small
 +81       non-USA       Small
 +82       non-USA     Compact
 +83       non-USA       Small
 +84       non-USA       Small
 +85       non-USA      Sporty
 +86       non-USA     Midsize
 +87       non-USA         Van
 +88       non-USA       Small
 +89       non-USA         Van
 +90       non-USA     Compact
 +91       non-USA      Sporty
 +92       non-USA     Compact
 +93       non-USA     Midsize
 +> cartbl <- table(cardata)
 +> cartbl
 +             Cars93.Type
 +Cars93.Origin Compact Large Midsize Small Sporty Van
 +      USA              11      10          8   5
 +      non-USA                12    14      6   4
 +> summary(cartbl)
 +Number of cases in table: 93 
 +Number of factors: 2 
 +Test for independence of all factors:
 + Chisq = 14.08, df = 5, p-value = 0.01511
 + Chi-squared approximation may be incorrect
  
 +> chisq.test(cartbl)
 +
 + Pearson's Chi-squared test
 +
 +data:  cartbl
 +X-squared = 14.08, df = 5, p-value = 0.01511
 +
 +Warning message:
 +In chisq.test(cartbl) : 카이제곱 approximation은 정확하지 않을수도 있습니다
 +
 +</code>
 ====== Calculating Quantiles (and Quartiles) of a Dataset ====== ====== Calculating Quantiles (and Quartiles) of a Dataset ======
  
Line 199: Line 320:
  
 <code>> dur <- faithful$eruptions <code>> dur <- faithful$eruptions
 +> dur > mean(dur)
 +  [1]  TRUE FALSE FALSE FALSE  TRUE FALSE  TRUE  TRUE FALSE  TRUE FALSE  TRUE  TRUE FALSE
 + [15]  TRUE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE  TRUE
 + [29]  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE FALSE FALSE  TRUE FALSE  TRUE  TRUE FALSE
 + [43]  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE  TRUE FALSE  TRUE FALSE  TRUE
 + [57]  TRUE FALSE  TRUE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE  TRUE  TRUE FALSE  TRUE
 + [71]  TRUE FALSE  TRUE  TRUE FALSE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE
 + [85]  TRUE  TRUE  TRUE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE  TRUE  TRUE
 + [99] FALSE  TRUE FALSE  TRUE FALSE  TRUE  TRUE FALSE  TRUE FALSE  TRUE  TRUE  TRUE FALSE
 +[113]  TRUE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE  TRUE FALSE  TRUE  TRUE
 +[127] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE
 +[141]  TRUE FALSE  TRUE  TRUE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE  TRUE FALSE  TRUE
 +[155]  TRUE  TRUE  TRUE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE  TRUE  TRUE FALSE  TRUE
 +[169] FALSE  TRUE FALSE FALSE  TRUE FALSE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE FALSE  TRUE
 +[183]  TRUE  TRUE FALSE  TRUE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE
 +[197]  TRUE  TRUE FALSE  TRUE FALSE  TRUE  TRUE FALSE  TRUE FALSE  TRUE  TRUE FALSE  TRUE
 +[211] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE
 +[225]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE FALSE  TRUE FALSE FALSE  TRUE
 +[239]  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE
 +[253]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE FALSE  TRUE FALSE FALSE
 +[267]  TRUE  TRUE FALSE  TRUE FALSE  TRUE 
 > mean(dur > mean(dur)) > mean(dur > mean(dur))
 [1] 0.6176471 [1] 0.6176471
Line 283: Line 425:
 > round(mean(zdur)) > round(mean(zdur))
 [1] 0 [1] 0
 +> round(sd(zdur))
 +[1] 1
 +
  
 </code> </code>
Line 352: Line 497:
 ====== Forming a Confidence Interval for a Mean ====== ====== Forming a Confidence Interval for a Mean ======
  
-<code>> s <- sd(x)+<code> 
 +> set.seed(1024) 
 +> x <- rnorm(50, mean=100, sd=15) 
 +> s <- sd(x)
 > m <- mean(x) > m <- mean(x)
 > n <- length(x) > n <- length(x)
Line 364: Line 512:
 > SE > SE
 [1] 2.458358 [1] 2.458358
-<- qt(.975, df=n-1)*SE+## qt fun: qt(prob, df) zscore 2점에 해당하는 점수는? 
 +qtv <- qt(.975, df=n-1) 
 +> qtv 
 +[1]  
 +## qtv는 2에 해당하는 95퍼센트 CL  
 +## 이 때의 CI는  
 +> E <- qtv*SE 
 > E > E
 [1] 4.940254 [1] 4.940254
Line 396: Line 550:
 W = 0.97415, p-value = 0.3386 W = 0.97415, p-value = 0.3386
 </code> </code>
 +The large p-value suggests the underlying population could be normally distributed. The next example reports a small p-value for y, so it is unlikely that this sample came from a normal population:
 +
 +normal distribution assumed -> var.equal=T
 +normal distribution not assumed -> var.equal=F
 +
  
 ====== Comparing the Means of Two Samples ====== ====== Comparing the Means of Two Samples ======
Line 448: Line 607:
 mpg.auto = mtcars[L,]$mpg  mpg.auto = mtcars[L,]$mpg 
 mpg.auto                    # automatic transmission mileage  mpg.auto                    # automatic transmission mileage 
- [1] 21.4 18.7 18.1 14.3 24.4 ...+ [1] 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4 10.4 14.7 21.5 15.5 15.2 
 +[18] 13.3 19.2
  
 mpg.manual = mtcars[!L,]$mpg  mpg.manual = mtcars[!L,]$mpg 
 mpg.manual                  # manual transmission mileage  mpg.manual                  # manual transmission mileage 
- [1] 21.0 21.0 22.8 32.4 30.4 ...+ [1] 21.0 21.0 22.8 32.4 30.4 33.9 27.3 26.0 30.4 15.8 19.7 15.0 21.4
  
 t.test(mpg.auto, mpg.manual) t.test(mpg.auto, mpg.manual)
Line 466: Line 626:
 mean of x mean of y  mean of x mean of y 
  17.14737  24.39231   17.14737  24.39231 
 +</code>
  
 +OR
 +
 +<code>> t.test(mtcars$mpg~mtcars$am)
 +
 + Welch Two Sample t-test
 +
 +data:  mtcars$mpg by mtcars$am
 +t = -3.7671, df = 18.332, p-value = 0.001374
 +alternative hypothesis: true difference in means is not equal to 0
 +95 percent confidence interval:
 + -11.280194  -3.209684
 +sample estimates:
 +mean in group 0 mean in group 1 
 +       17.14737        24.39231 
 </code> </code>
 +
  
 Another eg. Another eg.
  
-<code>a = c(175, 168, 168, 190, 156, 181, 182, 175, 174, 179) +<code>> a = c(175, 168, 168, 190, 156, 181, 182, 175, 174, 179) 
-b = c(185, 169, 173, 173, 188, 186, 175, 174, 179, 180)+b = c(185, 169, 173, 173, 188, 186, 175, 174, 179, 180)
 </code> </code>
  
-<code>t.test(a,b, var.equal=TRUE, paired=FALSE)+<code>> t.test(a,b, var.equal=TRUE, paired=FALSE)
  
 Two Sample t-test Two Sample t-test
Line 489: Line 665:
  
  
-qt(0.975, 18)+qt(0.975, 18)
 [1] 2.100922 [1] 2.100922
 </code> </code>
  
-<code>var.test(a,b)+<code>> var.test(a,b)
  
      F test to compare two variances      F test to compare two variances
Line 507: Line 683:
  
  
-qf(0.95, 9, 9)+qf(0.95, 9, 9)
 [1] 3.178893 [1] 3.178893
  
Line 518: Line 694:
 ===== e.g., ===== ===== e.g., =====
 <code>> sleep <code>> sleep
-#>    extra group ID +>    extra group ID 
-#> 1    0.7     +> 1    0.7     
-#> 2   -1.6     +> 2   -1.6     
-#> 3   -0.2     +> 3   -0.2     
-#> 4   -1.2     +> 4   -1.2     
-#> 5   -0.1     +> 5   -0.1     
-#> 6    3.4     +> 6    3.4     
-#> 7    3.7     +> 7    3.7     
-#> 8    0.8     +> 8    0.8     
-#> 9    0.0     +> 9    0.0     
-#> 10   2.0     1 10 +> 10   2.0     1 10 
-#> 11   1.9     +> 11   1.9     
-#> 12   0.8     +> 12   0.8     
-#> 13   1.1     +> 13   1.1     
-#> 14   0.1     +> 14   0.1     
-#> 15  -0.1     +> 15  -0.1     
-#> 16   4.4     +> 16   4.4     
-#> 17   5.5     +> 17   5.5     
-#> 18   1.6     +> 18   1.6     
-#> 19   4.6     +> 19   4.6     
-#> 20   3.4     2 10+> 20   3.4     2 10
 </code> </code>
-<code>sleep_wide <- data.frame(+<code>> sleep_wide <- data.frame(
     ID=1:10,     ID=1:10,
     group1=sleep$extra[1:10],     group1=sleep$extra[1:10],
Line 546: Line 722:
 ) )
 sleep_wide sleep_wide
-#>    ID group1 group2 +>    ID group1 group2 
-#> 1      0.7    1.9 +> 1      0.7    1.9 
-#> 2     -1.6    0.8 +> 2     -1.6    0.8 
-#> 3     -0.2    1.1 +> 3     -0.2    1.1 
-#> 4     -1.2    0.1 +> 4     -1.2    0.1 
-#> 5     -0.1   -0.1 +> 5     -0.1   -0.1 
-#> 6      3.4    4.4 +> 6      3.4    4.4 
-#> 7      3.7    5.5 +> 7      3.7    5.5 
-#> 8      0.8    1.6 +> 8      0.8    1.6 
-#> 9      0.0    4.6 +> 9      0.0    4.6 
-#> 10 10    2.0    3.4+> 10 10    2.0    3.4
 </code> </code>
 Ignore the ID variable for a convenience. Ignore the ID variable for a convenience.
Line 563: Line 739:
 # Welch t-test # Welch t-test
 t.test(extra ~ group, sleep) t.test(extra ~ group, sleep)
- +>  
-#>  +> Welch Two Sample t-test 
-#> Welch Two Sample t-test +>  
-#>  +> data:  extra by group 
-#> data:  extra by group +> t = -1.8608, df = 17.776, p-value = 0.07939 
-#> t = -1.8608, df = 17.776, p-value = 0.07939 +> alternative hypothesis: true difference in means is not equal to 0 
-#> alternative hypothesis: true difference in means is not equal to 0 +> 95 percent confidence interval: 
-#> 95 percent confidence interval: +>  -3.3654832  0.2054832 
-#>  -3.3654832  0.2054832 +> sample estimates: 
-#> sample estimates: +> mean in group 1 mean in group 2  
-#> mean in group 1 mean in group 2  +>            0.75            2.33
-#>            0.75            2.33+
  
 # Same for wide data (two separate vectors) # Same for wide data (two separate vectors)
-t.test(sleep_wide$group1, sleep_wide$group2)+t.test(sleep_wide$group1, sleep_wide$group2)
 </code> </code>
  
Line 584: Line 759:
 <code> <code>
 # Student t-test # Student t-test
-t.test(extra ~ group, sleep, var.equal=TRUE) +t.test(extra ~ group, sleep, var.equal=TRUE) 
-#>  +>  
-#> Two Sample t-test +> Two Sample t-test 
-#>  +>  
-#> data:  extra by group +> data:  extra by group 
-#> t = -1.8608, df = 18, p-value = 0.07919 +> t = -1.8608, df = 18, p-value = 0.07919 
-#> alternative hypothesis: true difference in means is not equal to 0 +> alternative hypothesis: true difference in means is not equal to 0 
-#> 95 percent confidence interval: +> 95 percent confidence interval: 
-#>  -3.363874  0.203874 +>  -3.363874  0.203874 
-#> sample estimates: +> sample estimates: 
-#> mean in group 1 mean in group 2  +> mean in group 1 mean in group 2  
-#           0.75            2.33+            0.75            2.33
 </code> </code>
  
-<code># Same for wide data (two separate vectors) +<code>#  Same for wide data (two separate vectors) 
-t.test(sleep_wide$group1, sleep_wide$group2, var.equal=TRUE)+t.test(sleep_wide$group1, sleep_wide$group2, var.equal=TRUE)
 </code> </code>
  
Line 608: Line 783:
 <code> <code>
 # Sort by group then ID # Sort by group then ID
-sleep <- sleep[order(sleep$group, sleep$ID), ]+sleep <- sleep[order(sleep$group, sleep$ID), ]
  
 # Paired t-test # Paired t-test
-t.test(extra ~ group, sleep, paired=TRUE) +t.test(extra ~ group, sleep, paired=TRUE) 
-#>  +  
-#>  Paired t-test +  Paired t-test 
-#>  +  
-#> data:  extra by group + data:  extra by group 
-#> t = -4.0621, df = 9, p-value = 0.002833 + t = -4.0621, df = 9, p-value = 0.002833 
-#> alternative hypothesis: true difference in means is not equal to 0 + alternative hypothesis: true difference in means is not equal to 0 
-#> 95 percent confidence interval: + 95 percent confidence interval: 
-#>  -2.4598858 -0.7001142 +  -2.4598858 -0.7001142 
-#> sample estimates: + sample estimates: 
-#> mean of the differences  + mean of the differences  
-#>                   -1.58+                   -1.58
 </code> </code>
  
 <code># Same for wide data (two separate vectors) <code># Same for wide data (two separate vectors)
-t.test(sleep.wide$group1, sleep.wide$group2, paired=TRUE)+t.test(sleep.wide$group1, sleep.wide$group2, paired=TRUE) 
 + 
 + Paired t-test 
 + 
 +data:  sleep_wide$group1 and sleep_wide$group2 
 +t = -4.0621, df = 9, p-value = 0.002833 
 +alternative hypothesis: true difference in means is not equal to 0 
 +95 percent confidence interval: 
 + -2.4598858 -0.7001142 
 +sample estimates: 
 +mean of the differences  
 +                  -1.58  
 </code> </code>
  
 The paired t-test is equivalent to testing whether difference between each pair of observations has a population mean of 0. (See below for comparing a single group to a population mean.) The paired t-test is equivalent to testing whether difference between each pair of observations has a population mean of 0. (See below for comparing a single group to a population mean.)
  
-<code>t.test(sleep.wide$group1 - sleep.wide$group2, mu=0, var.equal=TRUE) +<code>> t.test(sleep_wide$group1 - sleep_wide$group2, mu=0, var.equal=TRUE) 
-#> Error in t.test(sleep.wide$group1 - sleep.wide$group2, mu 0var.equal = TRUE)object 'sleep.wide' not found+ 
 + One Sample t-test 
 + 
 +data:  sleep_wide$group1 - sleep_wide$group2 
 +t = -4.0621df 9p-value = 0.002833 
 +alternative hypothesis: true mean is not equal to 0 
 +95 percent confidence interval: 
 + -2.4598858 -0.7001142 
 +sample estimates: 
 +mean of x  
 +    -1.58  
 </code> </code>
  
Line 641: Line 839:
 <code> <code>
 t.test(sleep$extra, mu=0) t.test(sleep$extra, mu=0)
-#>  +>  
-#> One Sample t-test +> One Sample t-test 
-#>  +>  
-#> data:  sleep$extra +> data:  sleep$extra 
-#> t = 3.413, df = 19, p-value = 0.002918 +> t = 3.413, df = 19, p-value = 0.002918 
-#> alternative hypothesis: true mean is not equal to 0 +> alternative hypothesis: true mean is not equal to 0 
-#> 95 percent confidence interval: +> 95 percent confidence interval: 
-#>  0.5955845 2.4844155 +>  0.5955845 2.4844155 
-#> sample estimates: +> sample estimates: 
-#> mean of x  +> mean of x  
-#>      1.54+>      1.54
 </code> </code>
  
r/general_statistics.txt · Last modified: 2019/10/11 07:56 by hkimscil

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki