#-------------------------------------------------------------------------#
# 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. Java线程——线程之间的几点重要说明

    在Java中,可以通过配合调用Object对象的wait()方法和notify()方法或notifyAll()方法来实现线程间的通信.在线程中调用wait()方法,将阻塞等待其他线程的通知(其他线程调 ...

  2. Tooltips2

    #include<windows.h> #include<Commctrl.h> #include"resource.h" #pragma comment( ...

  3. POJ 1845 Sumdiv [素数分解 快速幂取模 二分求和等比数列]

    传送门:http://poj.org/problem?id=1845 大致题意: 求A^B的所有约数(即因子)之和,并对其取模 9901再输出. 解题基础: 1) 整数的唯一分解定理: 任意正整数都有 ...

  4. linux复制指定文件

    find /somedir -type f|xargs -I {} cp {} . find /somedir -name "*.txt"|xargs -I {} cp {} .

  5. 关于mysql数据库连接异常处理

    tomcat启动错误日志关键信息: 28-Aug-2019 14:22:55.014 SEVERE [localhost-startStop-1] org.apache.catalina.core.C ...

  6. D. New Year and Conference(区间交,线段树)

    题:https://codeforces.com/contest/1284/problem/D 题意:给定n个1对的时间断,我是这么理解的,甲去参加a时间段的讲座,乙去参加b时间段的讲座,然后若这n对 ...

  7. scala编程(九)——控制抽象

    减少代码重复 所有的函数都被分割成通用部分,它们在每次函数调用中都相同,以及非通用部分,在不同的函 数调用中可能会变化.通用部分是函数体,而非通用部分必须由参数提供.当你把函数值用做参数时,算法的非通 ...

  8. Downton Abbey

    1. 当女儿以为泰坦尼克号不会沉的时候,父亲用了一个有意思的比喻: - I thought it was supposed to be unsinkable. - Every mountain is ...

  9. 二十二、NFS服务深入讲解

    一.查看nfs服务: [root@nfsserve ~]# ps -ef|grep -E "rpc|nfs"rpc       1565     1  0 17:29 ?      ...

  10. python学习笔记(22)-os文件操作模块

    疑问: 如果打开操作一个文件,是用绝对路径好还是相对路径好? os模块,在lib下面,可以直接引入的,直接使用import. 一.新建一个目录,新建一个文件夹 import os #新建一个文件夹 o ...