Rã®ãªãã¸ã§ã¯ãæåããã°ã©ãã³ã°(S3ã¨S4ã¯ã©ã¹)
R Advent Calendar 2013 : ATND 17æ¥ç®ã®è¨äºã§ãã
Rã®ãªãã¸ã§ã¯ãæå
Rããªãã¸ã§ã¯ãæåã§æ¸ããã¨èãã¦ãThe Art of R Programmingãèªãã ã
javaã¨ãpythonã®ãªãã¸ã§ã¯ãæåã¨ã¯ã ãã¶éããã®ã§ãRã®ãªãã¸ã§ã¯ãæåã¯ä¸»ã«ããã²ã¨ã¤ã®é¢æ°ã§ããããªæ©è½ãå¼ã³åºããã¨ãããã¨ãå®ç¾ãã¦ãã(ã¸ã§ããªãã¯é¢æ°ã¨ãããªã¢ãã£ãºã ã¨ãå¼ã°ãã¦ããã¤)
ä¾ãã°ãRã®plot()ã¯ãå¼æ°ã«æ¸¡ããã¼ã¿ãã©ããªå½¢ããã¦ã¦ã大ä½ãããããã¦ããã便å©é¢æ°ããããããã©ããªå¼æ°ã§ããã£ãã¼ã£ã¦ããå¨ã®å¤§ãããã¤ãã¸ã§ããªãã¯é¢æ°ã
試ãã«ãplotã®ä¸ãè¦ã¦ã¿ãã¨
methods(plot) [1] plot.acf* plot.cat plot.data.frame* [4] plot.decomposed.ts* plot.default plot.dendrogram* [7] plot.density plot.ecdf plot.factor* [10] plot.formula* plot.function plot.hclust* [13] plot.histogram* plot.HoltWinters* plot.isoreg* [16] plot.lm plot.medpolish* plot.mlm [19] plot.ppr* plot.prcomp* plot.princomp* [22] plot.profile.nls* plot.spec plot.stepfun [25] plot.stl* plot.table* plot.ts [28] plot.tskernel* plot.TukeyHSD Non-visible functions are asterisked
試ãã«ç·å½¢ã¢ãã«lm()ãä½ã£ã¦class()é¢æ°ã使ãã¨ã"lm"ã¨ããã¯ã©ã¹ãè¿ã£ã¦ãããplot()ã¯ããã«å¯¾å¿ããplot.lm()ãå®è¡ããã
model.lm <- lm(c(2, 4, 7) ~ c(1, 2, 3)) class(model.lm) [1] "lm" plot(model.lm) âå®éã«å¼ã°ããã®ã¯plot.lm(model.lm)
plotèªä½ã¯æç»ã®å®è£
ããã£ã¦ããªãã¦ãå¼æ°ã®ã¯ã©ã¹ã«å¿ããé¢æ°ãå®è¡ããã¨ããæãã
ãã±ã¢ã³ãã¬ã¼ãã¼èªèº«ã¯æ¦ããªãã§ãç¸æã«å¿ãã¦ãããã£ãã«ãã¥ã¦ï¼ã£ã¦ããã®ã¨åãã§ããª(éã)
ããããé¢æ°ã®å®è£
ã«ãS3ã¯ã©ã¹ã¨S4ã¯ã©ã¹ã使ããã¦ããã
主ã«ä½¿ããã¦ããã®ã¯S3ã§ãS3ã®å®å
¨æ§ãå¼±ããããªããã¨ãããã¨ã§ä½ãããã®ãS4ã
S3ã¯ã©ã¹
S3ã¯ã©ã¹ãä½ã£ã¦ã¿ãã
dog <- list(åå="Taro", 種é¡="dog") dog $åå [1] "Taro" $ç¨®é¡ [1] "dog" dog <- list(åå="Taro", 種="dog") class(dog) <- 'animal' class(dog) [1] "animal"
ããã§ãanimalã¯ã©ã¹ã®dogãªãã¸ã§ã¯ããã§ãããdogãªãã¸ã§ã¯ããplotã«ä½¿ãã¨ãplotã¯animalã¯ã©ã¹ã«å¯¾å¿ããé¢æ°ããã£ã¦ããªãã®ã§ãã¨ã©ã¼ã«ãªãã
plot.animalé¢æ°ãå®è£
ããã°ãplotã®é
ä¸ã«plot.animalãä½ããã¦ãplot(dog)ãå®è¡ãããã
plot(dog) 以ä¸ã«ã¨ã©ã¼ xy.coords(x, y, xlabel, ylabel, log) : 'x' is a list, but does not have components 'x' and 'y' plot.animal <- function(animal) { cat(animal$åå, '\n') cat(animal$種, '\n') } plot(dog) Taro dog
ã¾ãããã®animalã¯ã©ã¹ã¯ä½¿ãç©ã«ãªããªããã©ãèªåã§ä½ã£ããã¼ã¿åã«å¯¾å¿ãã俺俺plotãä½ãããã¨ãã«ã¯ä¾¿å©ã¨æãã
åå©ç¨æ§ã¨ãã«ãã»ã«åãå®ç¾ããã¦ããã®ã§ãã¾ããªãã¸ã§ã¯ãæåã¨å¼ã¹ãã®ãã¨ã
(ä¸å¿ãç¶æ¿ããã)
S4ã¯ã©ã¹
ãã£ãã®ä¾ã§ããã¨ãS3ã¯ã©ã¹ã¯å®å ¨æ§ãå¾®å¦ãã¨ããã®ãã¯ã©ã¹ã®æå®ãå¾ä»ããªã®ã§ãã§ããããªãã¼ã¿ã«animalã¯ã©ã¹ãæå®ãããã¨ãã§ãã¡ããã
dog <- c(1, 2, 3) class(dog) <- 'animal' class(dog) [1] "animal"
ããã ã¨plot.animalããã¨ãã«ãååã¨ã種ããªããã¼ã¨ã¨ã©ã¼ãã§ãã
ã¨ããããã§ãã¯ã©ã¹ãå®ç¾©ããã¨ãã«ãã¼ã¿ãå®ç¾©ãã¦ããã¾ãããã¨ããã®ãS4ã¯ã©ã¹ã
setClass()ã使ã£ã¦å®ç¾©ããã
setClass("animal", representation( åå="character", 種="character", å¹´="numeric") )
ãªãã¸ã§ã¯ããä½ãã¨ãã¯new()ã使ãã(ãªãã¸ã§ã¯ãæåã£ã½ãï¼)
dog <- new("animal", åå="Taro", 種="dog", å¹´=6) dog <- new("animal", åå="Taro", 種="dog", å¹´="6") 以ä¸ã«ã¨ã©ã¼ validObject(.Object) : ä¸æ£ãªã¯ã©ã¹ âanimalâ ãªãã¸ã§ã¯ã : invalid object for slot "å¹´" in class "animal": got class "character", should be or extend class "numeric"
åãééã£ã¦ããã¨ãã¯ãã¨ã©ã¼ã«ãªã£ã¦ãããã
ã¾ã¨ã
S3ã¨S4ã¯ã©ã£ã¡ãåªç§ã¨ãããã¨ã¯è¨ããªãã使ãã©ããã«ä¾ãã®ã§ã
ã§ãRã¯ã¤ã³ã¿ã©ã¯ãã£ãã«ä½¿ããã¨ãå¤ãã¨æãã®ã§ãç°¡åã«ä½ããS3ã§ååã ã¨æãã
S3ã¨S4ã¯ããªãã¸ã§ã¯ããé¢æ°ã¨ãã«é©ç¨ããã¨ãã«æããããããªãã¸ã§ã¯ããä½æããã¨ãã«æããããã¨ããéãã ããªæ°ãããããããããè¨èªä»æ§ãããããããªãã¦ãåãããã®ãRã®ããã¨ããã§ã¯ãããã
ããããã®ããããã ãã©ããªãã¸ã§ã¯ãæåã§æ¸ãããã£ããpython(ry
ã¾ãããã¡ããã¡ãåããã®ã ã¨æã£ã¦ãplotãsummaryã®é°ã§ãplot.lmãsummary.aovéãé å¼µã£ã¦ããã¨ãããã¨ã«åæãæãã¦ããã ããã°ååãªããããªããã¨ã