c:ma:2019:lecturer
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| c:ma:2019:lecturer [2019/10/01 23:06] – [anova: mtcars] hkimscil | c:ma:2019:lecturer [2019/10/04 11:28] (current) – [anova: mtcars] hkimscil | ||
|---|---|---|---|
| Line 93: | Line 93: | ||
| [13] 21.4 | [13] 21.4 | ||
| - | > data.frame(mdata) | ||
| - | Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : | ||
| - | arguments imply differing number of rows: 19, 13 | ||
| > stack(mdata) | > stack(mdata) | ||
| | | ||
| Line 130: | Line 127: | ||
| 31 | 31 | ||
| 32 | 32 | ||
| - | > mdat | ||
| - | Error: object ' | ||
| > mdata | > mdata | ||
| $`0` | $`0` | ||
| Line 141: | Line 136: | ||
| [13] 21.4 | [13] 21.4 | ||
| - | > mdata[[1\]] | ||
| - | Error: unexpected input in " | ||
| - | > mdata[[1]] | ||
| - | [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 | ||
| - | [13] 10.4 14.7 21.5 15.5 15.2 13.3 19.2 | ||
| - | > var(mdata[[1]]) | ||
| - | [1] 14.6993 | ||
| - | > length(mdata[[1]]) | ||
| - | [1] 19 | ||
| - | > md.df <- length(mdata[[1]]) -1 | ||
| - | > md.df | ||
| - | [1] 18 | ||
| - | > md.n <- length(mdata[[1]]) | ||
| - | > md.var <- var(mdata[[1]]) | ||
| - | > md.var | ||
| - | [1] 14.6993 | ||
| - | > md.ss <- md.var*md.df | ||
| - | > md.ss | ||
| - | [1] 264.5874 | ||
| - | > md2.df <- length(mdata[[2]]) -1 | ||
| - | > md2.n <- length(mdata[[2]]) | ||
| - | > md2.var <- var(mdata[[2]]) | ||
| - | > md2.ss <- md2.var*md2.df | ||
| - | > md.ss+md2.ss | ||
| - | [1] 720.8966 | ||
| - | > (md.ss+md2.ss)/ | ||
| - | [1] 24.02989 | ||
| - | > pvar <- sqrt((md.ss+md2.ss)/ | ||
| - | > pvar | ||
| - | [1] 4.902029 | ||
| - | > pvar <- (md.ss+md2.ss)/ | ||
| - | > pvar/nd.n | ||
| - | Error: object ' | ||
| - | > pvar/md.n | ||
| - | [1] 1.264731 | ||
| - | > pvar/md2.n | ||
| - | [1] 1.848453 | ||
| - | > a <- pvar/md.n | ||
| - | > b <- pvar/md2.n | ||
| - | > tscore <- sqrt(a+b) | ||
| - | > tscore | ||
| - | [1] 1.764422 | ||
| > t.test(mpg~am, | > t.test(mpg~am, | ||
| Line 196: | Line 149: | ||
| | | ||
| - | > t.test(mpg~am, | ||
| - | |||
| - | Welch Two Sample t-test | ||
| - | |||
| - | data: mpg by 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: | ||
| - | | ||
| - | sample estimates: | ||
| - | mean in group 0 mean in group 1 | ||
| - | | ||
| - | |||
| - | > ?t.test | ||
| > t.test(mpg~am, | > t.test(mpg~am, | ||
| Line 291: | Line 230: | ||
| | | ||
| } | } | ||
| - | stats <- stats4each(mtcars$hp, mtcars$cyl) | + | |
| + | library(MASS) | ||
| + | |||
| + | tempd <- iris | ||
| + | x <- tempd$Species | ||
| + | y <- tempd$Sepal.Width | ||
| + | |||
| + | tempd <- mtcars | ||
| + | x <- tempd$gear | ||
| + | y <- tempd$mpg | ||
| + | |||
| + | tempd <- mtcars | ||
| + | x <- tempd$am | ||
| + | y <- tempd$mpg | ||
| + | |||
| + | |||
| + | x <- factor(x) | ||
| + | dfbetween <- nlevels(x)-1 | ||
| + | |||
| + | stats <- stats4each(y, | ||
| stats | stats | ||
| sswithin <- sum(stats[5, | sswithin <- sum(stats[5, | ||
| - | sstotal <- var(mtcars$hp)*(length(mtcars$hp)-1) | + | sstotal <- var(y)*(length(y)-1) |
| ssbetween <- sstotal-sswithin | ssbetween <- sstotal-sswithin | ||
| - | sswithin | + | round(sswithin,2) |
| - | ssbetween | + | round(ssbetween,2) |
| - | sstotal | + | round(sstotal,2) |
| dfwithin <- sum(stats[4, | dfwithin <- sum(stats[4, | ||
| - | dfbetween <- 3-1 | + | dftotal <- length(y)-1 |
| - | dftotal <- length(mtcars$hp)-1 | + | |
| dfwithin | dfwithin | ||
| Line 314: | Line 271: | ||
| mstotal <- sstotal / dftotal | mstotal <- sstotal / dftotal | ||
| - | round(mswithin, | + | round(mswithin, |
| - | round(msbetween, | + | round(msbetween, |
| - | round(mstotal, | + | round(mstotal, |
| fval <- round(msbetween/ | fval <- round(msbetween/ | ||
| fval | fval | ||
| + | siglevel <- pf(q=fval, df1=dfbetween, | ||
| + | siglevel | ||
| - | mod <- aov(mtcars$hp~mtcars$cyl, data=mtcars) | + | mod <- aov(y~x, data=tempd) |
| summary(mod) | summary(mod) | ||
| </ | </ | ||
| + | ====== cor ====== | ||
| + | < | ||
| + | attach(mtcars) | ||
| + | cor(mpg, hp) | ||
| + | mycor <- cov(mpg, | ||
| + | mycor | ||
| + | |||
| + | sp <- cov(mpg, | ||
| + | ssx <- var(mpg)*(length(mtcars$mpg)-1) | ||
| + | ssy <- var(hp)*(length(mtcars$hp)-1) | ||
| + | |||
| + | mycor2 <- sp/ | ||
| + | mycor2 | ||
| + | |||
| + | mycor2 == mycor | ||
| + | mycor == cor(mpg,hp) | ||
| + | mycor2 == cor(mpg,hp) | ||
| + | |||
| + | </ | ||
c/ma/2019/lecturer.1569938800.txt.gz · Last modified: by hkimscil
