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
Next revisionBoth sides next revision
r:general_statistics [2017/10/30 09:12] – [Converting Data to Z-Scores] hkimscilr:general_statistics [2019/10/11 07:46] – [Forming a Confidence Interval for a Mean] hkimscil
Line 181: 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 377: 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 478: Line 601:
 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 517: Line 641:
 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 535: Line 659:
  
  
-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 553: Line 677:
  
  
-qf(0.95, 9, 9)+qf(0.95, 9, 9)
 [1] 3.178893 [1] 3.178893
  
Line 564: Line 688:
 ===== 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 592: Line 716:
 ) )
 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 609: Line 733:
 # 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 630: Line 753:
 <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 654: Line 777:
 <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 687: Line 833:
 <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