R与面向对象统计分析 - S3、S4对象的创建和使用

Similar documents
個 人 的 手, 拉 著 瞎 子 的 手 把 他 帶 往 村 外 的 時 候, 對 於 瞎 子 來 講, 那 個 人 的 手 和 耶 穌 的 手 有 沒 有 區 別? 沒 有! 為 什 麼 沒 有 區 別? 因 為 對 於 一 個 瞎 子 來 說, 手 和 耳 朵 就 是 他 接 觸 世 界, 瞭

> attr(a,'class') <- 'foo' # 用 attr( ) 函数将 a 的类指定为 foo > class(a) [1] "foo" > attr(b,'class')<-c('baz','bam','bar') # 指定 b 的属于三个类 'baz','bam','bar' >

Microsoft Word - 物件導向編程精要.doc

旅 句 良 年 理 了 來 不 不 更 更 說 識 更 樓 歷 練 靈 旅 論 不 了 契 諒 老 老 老 不 勵 老 不 良 論 漏 不 老 老 不 勵 不 了 了 老 論 利 行 老 見 不 見 更 老 玲 歷 老 料 理

Microsoft Word - ch04三校.doc

Microsoft Word - p11.doc

Value Chain ~ (E-Business RD / Pre-Sales / Consultant) APS, Advanc

北京市基本医疗保险政策解答

医学科研方法

Corporate Social Responsibility CSR CSR CSR 1 2 ~ CSR 6 CSR 7 CSR 8 CSR 9 10 ~ CSR 14 CSR CSR 2013 A A 23.

<4D F736F F D20A441A67EB160A67EB4C1B2C4A451A44BA544A4E920C0B1BCBBA544C34420ADB9C2B32E646F63>

Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Referenc

untitled

Wy159.mps

電機工程系認可證照清單 /7/1

<4D F736F F F696E74202D20C8EDBCFEBCDCB9B9CAA6D1D0D0DEBDB2D7F92E707074>

000

untitled

A Community Guide to Environmental Health

融通开放式基金月刊(2009年4月).doc

Microsoft Word - 特种水产饲料行业研究报告.docx

标题

Microsoft PowerPoint - plan08.ppt

untitled


AM101-1(中文)11.cdr

Strings

)

統計名詞0925

五 食 品 安 全 要 做 到 哪 三 证 五 防? 三 证 是 食 品 流 通 许 可 证 餐 饮 服 务 许 可 证 健 康 合 格 证 五 防 是 防 鼠 防 蝇 防 尘 防 腐 防 虫 六 国 家 卫 生 县 城 的 称 号 是 永 久 性 的 吗? 国 家 卫 生 县 城 不 是 终 生


K301Q-D VRT中英文说明书141009

市 立 永 平 高 中 無 填 報 無 填 報 (02) 市 立 樹 林 高 中 已 填 報 已 填 報 (02) 市 立 明 德 高 中 已 填 報 (02) 市 立 秀 峰 高 中 已 填 報

2. 禁 止 母 乳 代 用 品 之 促 銷 活 動, 以 及 不 得 以 贊 助 試 用 或 免 費 等 方 式, 取 得 奶 瓶 及 安 撫 奶 嘴 認 證 說 明 以 贊 助 試 用 或 免 費 等 方 式, 取 得 奶 瓶 及 安 撫 奶 嘴, 並 在 婦 產 科 門 診 兒 科 門 診 產

EN_110PAX4_QRG.fm

交易所网站定位.doc

兩岸青年人國際觀比較分析

untitled

上海浦~1

Microsoft Word 级第二专业学士学位培养计划.doc

金 陵 饭 店 中 兴 华 已 报 备 按 照 国 资 委 要 求 定 期 轮 换 天 衡 已 报 备 按 照 国 资 委 要 求 定 期 轮 换 *ST 中 富 中 喜 已 报 备 业 务 约 定 书 到 期 普

辉 丰 股 份 重 大 事 项, 特 停 南 方 轴 承 临 时 停 牌 德 力 股 份 临 时 停 牌 瑞 丰 光 电 临 时 停 牌 联 建 光 电 临 时 停 牌 卡 奴 迪 路 临 时 停 牌

东 华 能 源 江 苏 苏 亚 金 诚 已 报 备 因 地 域 及 审 计 时 间 安 排 等 原 因 中 兴 华 已 报 备 客 户 重 新 选 聘 会 计 师 事 务 所 亿 帆 鑫 富 立 信 已 报 备 客

昆 明 机 床 瑞 华 已 报 备 前 任 服 务 年 限 较 长 毕 马 威 华 振 已 报 备 未 与 客 户 未 就 2015 年 审 计 收 费 达 成 一 致 意 见 中 国 核 电 天 健 已 报 备 定

光 一 科 技 重 大 事 项, 特 停 茂 业 商 业 重 要 事 项 未 公 告, 连 续 停 牌 浙 富 控 股 重 大 事 项, 特 停 键 桥 通 讯 重 大 事 项, 特 停 黑 牛 食 品 重 大 事 项, 特 停

郑 州 煤 电 重 要 事 项 未 公 告, 连 续 停 牌 金 圆 股 份 重 大 事 项, 特 停 永 鼎 股 份 重 要 事 项 未 公 告, 连 续 停 牌 长 城 影 视 临 时 停 牌 天 兴 仪 表 临 时 停 牌

Untitled Document

商 业 城 大 华 标 准 70 万 70 万 驰 宏 锌 锗 瑞 华 标 准 140 万 150 万 亚 星 锚 链 江 苏 公 证 天 业 标 准 80 万 80

欢迎辞

日 涨 幅 偏 离 值 达 到 7% 的 前 五 只 证 券 : 温 氏 股 份 ( 代 码 ) 涨 幅 偏 离 值 :11.68% 成 交 量 :1752 万 股 成 交 金 额 : 万 元 机 构 专 用 机 构 专 用

上市公司股东大会投票信息公告( )

股票代码: 股票简称:*ST新梅 编号:临

金 利 科 技 临 时 停 牌 凤 凰 光 学 重 要 事 项 未 公 告, 连 续 停 牌 安 源 煤 业 重 要 事 项 未 公 告, 连 续 停 牌 万 泽 股 份 临 时 停 牌 爱 康 科 技 重 大 事 项, 特 停

卧 龙 地 产 重 要 事 项 未 公 告, 连 续 停 牌 春 兴 精 工 临 时 停 牌 *ST 沧 大 重 要 事 项 未 公 告, 连 续 停 牌 天 地 源 重 要 事 项 未 公 告, 连 续 停 牌 汇 冠 股 份

金 圆 股 份 重 大 事 项, 特 停 长 城 影 视 临 时 停 牌 天 兴 仪 表 临 时 停 牌 商 赢 环 球 重 要 事 项 未 公 告, 连 续 停 牌 荣 安 地 产 临 时 停 牌 中 南 文 化

证券代码:000776   股票简称:延边公路   编号:2003-00

EJB-Programming-4-cn.doc

中 国 证 券 监 督 管 理 委 员 会 : 根 据 贵 会 2015 年 12 月 9 日 签 发 的 中 国 证 监 会 行 政 许 可 项 目 审 查 一 次 反 馈 意 见 通 知 书 ( 号 )( 以 下 简 称 反 馈 意 见 ) 的 要 求, 无 锡 市 太 极 实 业

,, 2

Microsoft Word - A doc

EJB-Programming-3.PDF

WWW PHP

epub83-1

11第十一章階層線性模式.DOC

建築工程品質管理案例研討

中 国 证 券 监 督 管 理 委 员 会 : 2016 年 3 月 24 日, 海 南 海 航 基 础 设 施 投 资 集 团 股 份 有 限 公 司 ( 以 下 简 称 海 航 基 础 公 司 本 公 司 或 上 市 公 司 ) 收 到 中 国 证 券 监 督 管 理 委 员 会 ( 以 下 简

~ 10 2 P Y i t = my i t W Y i t 1000 PY i t Y t i W Y i t t i m Y i t t i 15 ~ 49 1 Y Y Y 15 ~ j j t j t = j P i t i = 15 P n i t n Y


ebook55-13

天 主 教 輔 仁 大 學 社 會 學 系 學 士 論 文 百 善 孝 為 先? 奉 養 父 母 與 接 受 子 女 奉 養 之 態 度 及 影 響 因 素 : 跨 時 趨 勢 分 析 Changes in attitude toward adult children's responsibilit

untitled


度 身 體 活 動 量 ; 芬 蘭 幼 兒 呈 現 中 度 身 體 活 動 量 之 比 例 高 於 臺 灣 幼 兒 (5) 幼 兒 在 投 入 度 方 面 亦 達 顯 著 差 異 (χ²=185.35, p <.001), 芬 蘭 與 臺 灣 幼 兒 多 半 表 現 出 中 度 投 入 與 高 度

Chapter 9: Objects and Classes

<4D F736F F D20C9EEDBDAD6A4BCE0BED6BBE1BCC6BCE0B9DCB9A4D7F7CDA8D1B65F C4EAB5DA35C6DAA3ACD7DCB5DA3330C6DA5F>

(156) / Spurious Regression Unit Root Test Cointergration TestVector Error Correction Model Granger / /

南華大學數位論文


运动员治疗用药豁免申报审批办法


2005硕士论文模版

1-1 database columnrow record field 不 DBMS Access Paradox SQL Server Linux MySQL Oracle IBM Informix IBM DB2 Sybase 1-2

D C 93 2

Microsoft Word - 3D手册2.doc

技 巧 5: 避 免 除 以 0 的 運 算 在 做 除 的 運 算 時, 先 檢 查 除 數 的 數 值, 避 免 有 除 以 0 的 情 況 若 運 算 中 除 數 為 0,SAS 會 在 LOG 中 註 記 提 醒 並 將 運 算 結 果 設 定 為 遺 漏 值, 減 慢 程 式 的 執 行

Microsoft Word - A _ doc

untitled

59 1 CSpace 2 CSpace CSpace URL CSpace 1 CSpace URL 2 Lucene 3 ID 4 ID Web 1. 2 CSpace LireSolr 3 LireSolr 3 Web LireSolr ID

玉田國小100學年度上學期品德教育月主題

《金融评论》投稿 封面

信 息 化 的 整 合 过 程 要 分 为 若 干 阶 段 来 实 现 1. IDC 建 设 阶 段 最 初 需 要 建 设 的 是 一 个 全 校 统 一 的 数 据 中 心, 将 运 行 的 设 备 和 管 理 环 境 进 行 简 单 的 物 理 合 并, 这 样 做 的 好 处 在 于 降 低


无类继承.key

% % % % % % ~

Microsoft PowerPoint - L17_Inheritance_v4.pptx

厦 门 大 学 学 位 论 文 原 创 性 声 明 本 人 呈 交 的 学 位 论 文 是 本 人 在 导 师 指 导 下, 独 立 完 成 的 研 究 成 果 本 人 在 论 文 写 作 中 参 考 其 他 个 人 或 集 体 已 经 发 表 的 研 究 成 果, 均 在 文 中 以 适 当 方

Vol. 15 No. 1 JOURNAL OF HARBIN UNIVERSITY OF SCIENCE AND TECHNOLOGY Feb O21 A

國家圖書館典藏電子全文

(134) 臺 灣 銀 行 季 刊 第 六 十 一 卷 第 四 期 各 類 能 源 服 務 需 求 (Energy Service Needs) 之 推 估 以 上 能 源 服 務 需 求 的 推 估 將 作 為 MARKAL 模 型 之 輸 入 參 數, 其 推 估 之 正 確 性 攸 關 MAR


Transcription:

R S3 S4 2012 11 4

: S3 : S4 S3 S4

: S3 : S4 S3 S4

There is an important difference in philosophy between S (and hence R) and the other main statistical systems In S a statistical analysis is normally done as a series of steps, with intermediate results being stored in objects An Introduction to R

,,

,,

,,,

R (object-oriented programming), (encapsulate),,

R (object-oriented programming), (encapsulate),, R,

R (object-oriented programming), (encapsulate),, R, R,

R (object-oriented programming), (encapsulate),, R, R, R

(class) (method)

(class) (method), (instance)

(class) (method), (instance)

: S3 : S4 S3 S4

S3 S3

S3 S3,

S3 S3, R print, summary plot, summary(),

x <- rep(0:1, c(10, 20)) summary(x) ## Min 1st Qu Median Mean 3rd Qu Max ## 0000 0000 1000 0667 1000 1000 y <- asfactor(x) summary(y) ## 0 1 ## 10 20

S3 list class, foo class() x <- 1 attr(x, "class") <- "foo" x ## [1] 1 ## attr(,"class") ## [1] "foo" class(x) ## [1] "foo" x <- structure(1, class = "foo") x ## [1] 1 ## attr(,"class") ## [1] "foo" class(x) ## [1] "foo"

S3, class class(x) <- c("foo", "bar") class(x) ## [1] "foo" "bar"

(method dispatch) (generic function) :, UseMethod(), mean() mean ## function (x, ) ## UseMethod("mean") ## <bytecode: 0x102f0eab0> ## <environment: namespace:base>

UseMethod() class, genericclass UseMethod(), : meannumeric <- function(x, ) sum(x)/length(x) meandataframe <- function(x, ) sapply(x, mean, ) meanmatrix <- function(x, ) apply(x, 2, mean)

class c( foo, bar ), meanfoo, meanbar, meandefault bar <- function(x) UseMethod("bar", x) bardefault <- function(x) "default" bary <- function(x) "y" barz <- function(x) "z" foo <- structure(1, class = "nonsense") bar(foo) ## [1] "default" foo <- structure(1, class = c("y", "z")) bar(foo) ## [1] "y"

(Inheritance) class, S3 class NextMethod() barson <- function(x) c("i am son", NextMethod()) barfather <- function(x) c("i am father") foo <- structure(1, class = c("son", "father")) bar(foo) ## [1] "I am son" "I am father"

S3 S3 class,genericclass foo <- structure(1, class = "nonsense") bar(foo) ## [1] "default" barz(foo) ## [1] "z"

class class class() x <- 1 attr(x, "my_cool_class") <- "foo" x ## [1] 1 ## attr(,"my_cool_class") ## [1] "foo" class(x) ## [1] "numeric"

class, class class(foo) <- c("y", "z") bar(foo) ## [1] "y" class(foo) <- c("z", "y") bar(foo) ## [1] "z"

S3 ˆβ 0, ˆβ 1, cov( ˆβ 0, ˆβ x 1 ) = (xi x) 2 σ2, Var( ˆβ 0 ) = (1/n + Var( ˆβ 1 ) = x 2 (xi x) 2 )σ2 σ 2 (xi x) 2

fit <- lm(dist ~ speed, data = cars) sigma2 <- sd(fit$residuals)^2 cov <- -mean(cars$speed)/sum((cars$speed - mean(cars$speed))^2) * sigma2 var_beta0 <- (1/nrow(cars) + mean(cars$speed)^2/sum((cars$speed - mean(cars$speed))^2)) * sigma2 var_beta1 <- sigma2/sum((cars$speed - mean(cars$speed))^2) 1 2 1 4474-260 2-260 017

, summary() lm, summary() sigma2 <- summary(fit)$sigma^2

, summary() lm, summary() sigma2 <- summary(fit)$sigma^2 vcov() lm vcov(fit) ## (Intercept) speed ## (Intercept) 45677-26588 ## speed -2659 01727

co2d <- decompose(co2, type = "multiplicative") plot(co2d$x, ylim = c(0, 400), type = "l") lines(co2d$seasonal) lines(co2d$trend) lines(co2d$random) decompose() co2d$x 0 100 200 300 400 (ts) 1960 1970 1980 1990 Time

m <- cbind(co2d$x, co2d$seasonal, co2d$trend, co2d$random) co2mts <- ts(m) plot(co2mts) co2d$random co2d$trend co2d$seasonal co2d$x 0998 1000 1002 320 340 360 0990 1000 320 340 360 co2mts ts(),, plot() 0 100 200 300 400 Time

plot(co2d) Decomposition of multiplicative time series decomposedts plotdecomposedts() random observed trend 320 340 360 320 340 360 seasonal 0998 1000 1002 0990 1000 1960 1970 1980 1990 Time

S3 methods() S3 methods(genericfunction=predict) methods(class=lm)

S3 methods() S3 methods(genericfunction=predict) methods(class=lm) gets3method(f, class)

S3 methods() S3 methods(genericfunction=predict) methods(class=lm) gets3method(f, class), getanywhere()

: S3 : S4 S3 S4

S4 setclass() S4 representation (slot) setclass(class = "Person", representation(name = "character", age = "numeric"))

S4 S3, contains,, setclass(class = "Reporter", representation(title = "character"), contains = "Person")

new() S4, yuchen <- new("reporter", name = "yuchen", age = 22, title = 22) ## Error: invalid class "Reporter" object: invalid object for slot "title" in class "Reporter": got class "numeric", should be or extend class "character" yuchen <- new("reporter", name = "yuchen", age = 22, title = "R and OOP")

S3 $, S4 @ yuchen@name ## [1] "yuchen", slot() slot(yuchen, "age") ## [1] 22

, numeric, numeric(0), prototype setclass("person", representation(name = "character", age = "numeric"), prototype(name = NA_character_, age = NA_real_)) new("person", name = "yuchen")@age ## [1] NA

, validity CheckAge <- function(object) { if (object@age <= 0) { stop("age is negative") } } setclass("person", representation(name = "character", age = "numeric"), validity = CheckAge) new("person", age = -5) ## Error: Age is negative

S3, setgeneric(), standardgeneric() setgeneric("prepare", function(object) { }) standardgeneric("prepare")

setmethod(), signature() setmethod("prepare", signature(object = "Person"), function(object) { }) cat("got Materials\n") prepare(new("person")) ## Got Materials

callnextmethod() setmethod("prepare", signature(object = "Reporter"), function(object) { callnextmethod() cat("slides are ready\n") }) prepare(new("reporter")) ## Got Materials ## Slides are ready

S4 is(), getslots()

S4 is(), getslots() showmethods()

S4 is(), getslots() showmethods() Bioconductor Matrix S4,

Reference Class R5, 212,R5 R, Java C#?ReferenceClasses

Reference Class R5, 212,R5 R, Java C#?ReferenceClasses : Roo, proto mutatr

: S3 : S4 S3 S4

# S3 a1 <- a2 <- a3 <- a4 <- a5 <- 0 class(a1) <- "Instrument" class(a2) <- c("stringed", "Instrument") class(a3) <- c("wind", "Instrument") class(a4) <- c("brass", "Wind", "Instrument") class(a5) <- c("woodwind") # S4 setclass("instrument", representation("virtual", tune = "character")) setclass("stringed", representation("instrument")) setclass("wind", representation("instrument")) setclass("brass", contains = "Wind") setclass("woodwind", representation(tune = "character"))

# S3 play3 <- function(x, ) UseMethod("play3") play3instrument <- function(x) print("i am a Instrument") play3stringed <- function(x) print("i am a Stringed") play3default <- function(x) print("i don't know who I am") # S4 setgeneric("play4", function(object, ) standardgeneric("play4")) setmethod("play4", "Instrument", function(object) print(paste("play:", object@tune)))

play3(a1) ## [1] "I am a Instrument" play3(a2) ## [1] "I am a Stringed" play3(a3) ## [1] "I am a Instrument" play3(a4) ## [1] "I am a Instrument" play3(a5) ## [1] "I don't know who I am"

play4(new("stringed", tune = "I am a Stringed")) ## [1] "Play: I am a Stringed" play4(new("wind", tune = "I am a Wind")) ## [1] "Play: I am a Wind" play4(new("brass", tune = "I am a Brass")) ## [1] "Play: I am a Brass" play4(new("woodwind", tune = "I am a Woodwind")) ## Error: unable to find an inherited method for function 'play4' for signature '"Woodwind"'