User Tools

Site Tools


c:ma:2019:lecturer

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
c:ma:2019:lecturer [2019/10/01 23:10] – [anova: mtcars] hkimscilc: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)
    values ind    values ind
Line 130: Line 127:
 31   15.0   1 31   15.0   1
 32   21.4   1 32   21.4   1
-> mdat 
-Error: object 'mdat' not found 
 > mdata > mdata
 $`0` $`0`
Line 141: Line 136:
 [13] 21.4 [13] 21.4
  
-> mdata[[1\]] 
-Error: unexpected input in "mdata[[1\" 
-> 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)/(md.df+md2.df) 
-[1] 24.02989 
-> pvar <- sqrt((md.ss+md2.ss)/(md.df+md2.df)) 
-> pvar 
-[1] 4.902029 
-> pvar <- (md.ss+md2.ss)/(md.df+md2.df) 
-> pvar/nd.n 
-Error: object 'nd.n' not found 
-> 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, data=mtcars) > t.test(mpg~am, data=mtcars)
  
Line 196: Line 149:
        17.14737        24.39231         17.14737        24.39231 
  
-> t.test(mpg~am, data=mtcars, equal.var=T) 
- 
- 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: 
- -11.280194  -3.209684 
-sample estimates: 
-mean in group 0 mean in group 1  
-       17.14737        24.39231  
- 
-> ?t.test 
 > t.test(mpg~am, data=mtcars, var.equal=T) > t.test(mpg~am, data=mtcars, var.equal=T)
  
Line 291: Line 230:
    return(out)      return(out)  
 } }
-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, x)
 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
  
-round(sswithin,0+round(sswithin,2
-round(ssbetween,0+round(ssbetween,2
-round(sstotal,0)+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,0+round(mswithin,2
-round(msbetween,0+round(msbetween,2
-round(mstotal,0)+round(mstotal,2)
  
 fval <- round(msbetween/mswithin,2) fval <- round(msbetween/mswithin,2)
 fval fval
-siglevel <- pf(q=36.18, df1=2, df2=29, lower.tail=FALSE)+siglevel <- pf(q=fval, df1=dfbetween, df2=dfwithin, lower.tail=FALSE)
 siglevel siglevel
  
-mod <- aov(mtcars$hp~mtcars$cyl, data=mtcars)+mod <- aov(y~x, data=tempd)
 summary(mod) summary(mod)
  
 </code> </code>
  
 +====== cor ======
 +<code>
 +attach(mtcars)
 +cor(mpg, hp)
  
 +mycor <- cov(mpg,hp)/(sd(mpg)*sd(hp))
 +mycor
 +
 +sp <- cov(mpg,hp)*(length(mtcars$hp)-1)
 +ssx <- var(mpg)*(length(mtcars$mpg)-1)
 +ssy <- var(hp)*(length(mtcars$hp)-1)
 +
 +mycor2 <- sp/sqrt(ssx*ssy)
 +mycor2
 +
 +mycor2 == mycor
 +mycor == cor(mpg,hp)
 +mycor2 == cor(mpg,hp)
 +
 +</code>
  
c/ma/2019/lecturer.1569939022.txt.gz · Last modified: 2019/10/01 23:10 by hkimscil

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki