#-------------------------------------------------------------------------#
# R in Action (2nd ed): Chapter 12 #
# Resampling statistics and bootstrapping #
# requires packages coin, multcomp, vcd, MASS, lmPerm, boot #
# install.packages(c("coin","multcomp", "vcd", "MASS", "boot")) #
# Follow chapter instructions for installing lmPerm #
#-------------------------------------------------------------------------# par(ask=TRUE) # Listing 12.1 - t-test vs. oneway permutation test for the hypothetical data
library(coin)
score <- c(40, 57, 45, 55, 58, 57, 64, 55, 62, 65)
treatment <- factor(c(rep("A",5), rep("B",5)))
mydata <- data.frame(treatment, score)
t.test(score~treatment, data=mydata, var.equal=TRUE)
oneway_test(score~treatment, data=mydata, distribution="exact") # Wilcoxon Mann-Whitney U test
UScrime <- transform(UScrime, So = factor(So))
wilcox_test(Prob ~ So, data=UScrime, distribution="exact") # k sample test
library(multcomp)
set.seed(1234) # make results reproducible
oneway_test(response~trt, data=cholesterol,
distribution=approximate(B=9999)) # independence in contingency tables
library(coin)
library(vcd)
Arthritis <- transform(Arthritis,
Improved = as.factor(as.numeric(Improved)))
set.seed(1234)
chisq_test(Treatment~Improved, data=Arthritis,
distribution=approximate(B=9999)) # independence between numeric variables
states <- as.data.frame(state.x77)
set.seed(1234)
spearman_test(Illiteracy ~ Murder, data=states,
distribution=approximate(B=9999)) # dependent 2-sample and k-sample tests
library(coin)
library(MASS)
wilcoxsign_test(U1 ~ U2, data=UScrime, distribution="exact") # Listing 12.2 - Permutation tests for simple linear regression
library(lmPerm)
set.seed(1234)
fit <- lmp(weight ~ height, data=women, perm="Prob")
summary(fit) # Listing 12.3 - Permutation tests for polynomial regression
library(lmPerm)
set.seed(1234)
fit <- lmp(weight ~ height + I(height^2), data=women, perm="Prob")
summary(fit) # Listing 12.4 - Permutation tests for multiple regression
library(lmPerm)
set.seed(1234)
states <- as.data.frame(state.x77)
fit <- lmp(Murder ~ Population + Illiteracy+Income+Frost,data=states, perm="Prob")
summary(fit) # Listing 12.5 - Permutation test for One-Way ANOVA
library(lmPerm)
library(multcomp)
set.seed(1234)
fit <- lmp(response ~ trt, data=cholesterol, perm="Prob")
anova(fit) # Listing 12.6 - Permutation test for One-Way ANCOVA
library(lmPerm)
set.seed(1234)
fit <- lmp(weight ~ gesttime + dose, data=litter, perm="Prob")
anova(fit) # Listing 12.7 - Permutation test for Two-way ANOVA
library(lmPerm)
set.seed(1234)
fit <- lmp(len ~ supp*dose, data=ToothGrowth, perm="Prob")
anova(fit) # bootstrapping a single statistic (R2)
rsq <- function(formula, data, indices) {
d <- data[indices,]
fit <- lm(formula, data=d)
return(summary(fit)$r.square)
} library(boot)
set.seed(1234)
results <- boot(data=mtcars, statistic=rsq,
R=1000, formula=mpg~wt+disp)
print(results)
plot(results)
boot.ci(results, type=c("perc", "bca")) # bootstrapping several statistics (regression coefficients)
bs <- function(formula, data, indices) {
d <- data[indices,]
fit <- lm(formula, data=d)
return(coef(fit))
}
library(boot)
set.seed(1234)
results <- boot(data=mtcars, statistic=bs,
R=1000, formula=mpg~wt+disp) print(results)
plot(results, index=2)
boot.ci(results, type="bca", index=2)
boot.ci(results, type="bca", index=3)

吴裕雄--天生自然 R语言开发学习:重抽样与自助法(续一)的更多相关文章

  1. 吴裕雄--天生自然 R语言开发学习:基本图形(续二)

    #---------------------------------------------------------------# # R in Action (2nd ed): Chapter 6 ...

  2. 吴裕雄--天生自然 R语言开发学习:基本图形(续一)

    #---------------------------------------------------------------# # R in Action (2nd ed): Chapter 6 ...

  3. 吴裕雄--天生自然 R语言开发学习:高级数据管理(续三)

    #-----------------------------------# # R in Action (2nd ed): Chapter 5 # # Advanced data management ...

  4. 吴裕雄--天生自然 R语言开发学习:基本数据管理(续二)

    #---------------------------------------------------------# # R in Action (2nd ed): Chapter 4 # # Ba ...

  5. 吴裕雄--天生自然 R语言开发学习:广义线性模型(续一)

    #----------------------------------------------# # R in Action (2nd ed): Chapter 13 # # Generalized ...

  6. 吴裕雄--天生自然 R语言开发学习:中级绘图(续二)

    #------------------------------------------------------------------------------------# # R in Action ...

  7. 吴裕雄--天生自然 R语言开发学习:中级绘图(续一)

    #------------------------------------------------------------------------------------# # R in Action ...

  8. 吴裕雄--天生自然 R语言开发学习:功效分析(续一)

    #----------------------------------------# # R in Action (2nd ed): Chapter 10 # # Power analysis # # ...

  9. 吴裕雄--天生自然 R语言开发学习:基本统计分析(续三)

    #---------------------------------------------------------------------# # R in Action (2nd ed): Chap ...

  10. 吴裕雄--天生自然 R语言开发学习:基本图形(续三)

    #---------------------------------------------------------------# # R in Action (2nd ed): Chapter 6 ...

随机推荐

  1. 虚拟机virtualBox

    在笔记本了装了一个虚拟机, 并安装了Linux系统, 方便测试linux 命令. 考虑到不需要图形界面, 学习了用命令行操作虚拟机, 配置如下 linux 下安装openssh-server 虚拟机设 ...

  2. [Algo] 611. Compress String II

    Given a string, replace adjacent, repeated characters with the character followed by the number of r ...

  3. saturates|meteoric|enclose|marooned|predators|Pioneer community|salinization|condenser|embodied

    saturates渗透 meteoric蒸汽 enclose包围 Pioneer community 先锋群落 Climax community顶级群落 cumulative积累 Rebound 回弹 ...

  4. 可视化---seaborn

    变量说明 x,y,hue 数据集变量 变量名 date 数据集 数据集名 row,col 更多分类变量进行平铺显示 变量名 col_wrap 每行的最高平铺数 整数 estimator 在每个分类中进 ...

  5. Android studio 3.0安装与配置(看这一篇就够了)

    前言 为了完成数据库大作业,并充分利用学过的Java语言,决定开发一个简单完整成熟的安卓手机应用程序.于是下载安装Android Studio集成开发环境,第一次安装最新版本,因为墙的原因安装失败,第 ...

  6. 蓝桥杯 K好数(dp)

    Description 如果一个自然数N的K进制表示中任意的相邻的两位都不是相邻的数字,那么我们就说这个数是K好数.求L位K进制数中K好数的数目.例如K = 4,L = 2的时候,所有K好数为11.1 ...

  7. virtualbox Ubuntu拒绝root用户ssh远程登录

    http://www.bcty365.com/content-122-6241-1.html

  8. 树莓派docker搭建

    树莓派上 Docker 的安装和使用 Docker 是一个开源的应用容器引擎,可以让开发者打包他们的应用以及依赖包到一个轻量级.可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟 ...

  9. linux select函数

    /**两个线程一个负责监听客户端,一个负责读客户端请求. 服务器模型,*主控线程负责accept监听链接的客户端,*把客户端fd放入任务队列中(),分离子线程则从任务队列取出所有的*客户端描述加入se ...

  10. MQ消息队列的12点核心原理总结

    1. 消息生产者.消息者.队列 消息生产者Producer:发送消息到消息队列. 消息消费者Consumer:从消息队列接收消息. Broker:概念来自与Apache ActiveMQ,指MQ的服务 ...