#置换实验 Coin包
install.packages(c("coin"))#lmPerm包
install.packages("lmPerm")#独立两样本和K样本检验
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)

Two Sample t-test

data: score by treatment
t = -2.345, df = 8, p-value = 0.04705
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-19.0405455 -0.1594545
sample estimates:
mean in group A mean in group B
51.0 60.6

#置换T检验
library(coin)
oneway_test(score~treatment,data=mydata,distribution="exact")

Exact Two-Sample Fisher-Pitman Permutation Test

data: score by treatment (A, B)
Z = -1.9147, p-value = 0.07143
alternative hypothesis: true mu is not equal to 0

结果显著:P<0.05

结果不显著:P>0.05

上面这两个测试中所得的P值相差比较大,并且样本容易比较小,所以置换T检验更有说服力,因此我们认为改组样本得到的结果不显著P>0.05

#Wilcox.test
library(MASS)
UScrime<-transform(UScrime,So=factor(So))
wilcox_test(Prob~So,data=UScrime,distribution="exact")

Exact Wilcoxon-Mann-Whitney Test

data: Prob by So (0, 1)
Z = -3.7493, p-value = 8.488e-05
alternative hypothesis: true mu is not equal to 0

wilcox.test(Prob~So,data=UScrime,distribution="exact")

Wilcoxon rank sum test

data: Prob by So
W = 81, p-value = 8.488e-05
alternative hypothesis: true location shift is not equal to 0

#chisq_Test
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))

Approximative Pearson Chi-Squared Test

data: Treatment by Improved (1, 2, 3)
chi-squared = 13.055, p-value = 0.0018

#spearman.test 数值变量间的独立性
states<-as.data.frame(state.x77)
set.seed(1234)
spearman_test(Illiteracy~Murder,data=states,distribution=approximate(B=9999))

Approximative Spearman Correlation Test

data: Illiteracy by Murder
Z = 4.7065, p-value < 2.2e-16
alternative hypothesis: true rho is not equal to 0

#两样本和K样本相关性检验
library(coin)
library(MASS)
wilcoxsign_test(U1~U2,data=UScrime,distribution="exact")

Exact Wilcoxon-Pratt Signed-Rank Test

data: y by x (pos, neg)
stratified by block
Z = 5.9691, p-value = 1.421e-14
alternative hypothesis: true mu is not equal to 0

#lmPerm包的置换检验
library(lmPerm)#简单线性回归置换
set.seed(1234)
fit<-lmp(weight~height,data=women,perm="Prob")
summary(fit)

Call:
lmp(formula = weight ~ height, data = women, perm = "Prob")

Residuals:
Min 1Q Median 3Q Max
-1.7333 -1.1333 -0.3833 0.7417 3.1167

Coefficients:
Estimate Iter Pr(Prob)
height 3.45 5000 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 1.525 on 13 degrees of freedom
Multiple R-Squared: 0.991, Adjusted R-squared: 0.9903
F-statistic: 1433 on 1 and 13 DF, p-value: 1.091e-14

#多项式回归的置换检验
set.seed(1234)
fit<-lmp(weight~height+I(height^2),data=women,perm="Prob")
summary(fit)

Call:
lmp(formula = weight ~ height + I(height^2), data = women, perm = "Prob")

Residuals:
Min 1Q Median 3Q Max
-0.509405 -0.296105 -0.009405 0.286151 0.597059

Coefficients:
Estimate Iter Pr(Prob)
height -7.34832 5000 <2e-16 ***
I(height^2) 0.08306 5000 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.3841 on 12 degrees of freedom
Multiple R-Squared: 0.9995, Adjusted R-squared: 0.9994
F-statistic: 1.139e+04 on 2 and 12 DF, p-value: < 2.2e-16

#多元回归
library(lmPerm)
set.seed(1234)
states<-as.data.frame(state.x77)
fit<-lmp(Murder~Population+Illiteracy+Income+Frost,data=states,perm="Prob")
summary(fit)

Call:
lmp(formula = Murder ~ Population + Illiteracy + Income + Frost,
data = states, perm = "Prob")

Residuals:
Min 1Q Median 3Q Max
-4.79597 -1.64946 -0.08112 1.48150 7.62104

Coefficients:
Estimate Iter Pr(Prob)
Population 2.237e-04 51 1.0000
Illiteracy 4.143e+00 5000 0.0004 ***
Income 6.442e-05 51 1.0000
Frost 5.813e-04 51 0.8627
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 2.535 on 45 degrees of freedom
Multiple R-Squared: 0.567, Adjusted R-squared: 0.5285
F-statistic: 14.73 on 4 and 45 DF, p-value: 9.133e-08

#单因素方差分析和协方差分析
library(lmPerm)
library(multcomp)
set.seed(1234)
fit<-aovp(response~trt,data=cholesterol,perm="Prob")
anova(fit)

Analysis of Variance Table

Response: response
Df R Sum Sq R Mean Sq Iter Pr(Prob)
trt 4 1351.37 337.84 5000 < 2.2e-16 ***
Residuals 45 468.75 10.42
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

#双因素方差分析
library(lmPerm)
set.seed(1234)
fit<-aovp(len~supp*dose,data=ToothGrowth,perm="Prob")
anova(fit)

Analysis of Variance Table

Response: len
Df R Sum Sq R Mean Sq Iter Pr(Prob)
supp 1 205.35 205.35 5000 < 2e-16 ***
dose 1 2224.30 2224.30 5000 < 2e-16 ***
supp:dose 1 88.92 88.92 2032 0.04724 *
Residuals 56 933.63 16.67
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

#自助法
library(boot)
set.seed(1234)
rsq<-function(formula,data,indices){d<-data[indices,]fit<-lm(formula,data=d)return(summary(fit)$r.square)}results<-boot(data=mtcars,statistic=rsq,R=1000,formula=mpg~wt+disp)
print(results)boot.ci(results,type=c("perc","bca"))

BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
Based on 1000 bootstrap replicates

CALL :
boot.ci(boot.out = results, type = c("perc", "bca"))

Intervals :
Level Percentile BCa
95% ( 0.6838, 0.8833 ) ( 0.6344, 0.8549 )
Calculations and Intervals on Original Scale
Some BCa intervals may be unstable

#多个统计量的自助法
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=1)

转载于:https://www.cnblogs.com/GhostBear/p/7736915.html

R语言学习笔记(十):重抽样与自助法相关推荐

  1. R语言学习笔记——高级篇:第十四章-主成分分析和因子分析

    R语言 R语言学习笔记--高级篇:第十四章-主成分分析和因子分析 文章目录 R语言 前言 一.R中的主成分和因子分析 二.主成分分析 2.1.判断主成分的个数 2.2.提取主成分 2.3.主成分旋转 ...

  2. R语言学习笔记(1~3)

    R语言学习笔记(1~3) 一.R语言介绍 x <- rnorm(5) 创建了一个名为x的向量对象,它包含5个来自标准正态分布的随机偏差. 1.1 注释 由符号#开头. #函数c()以向量的形式输 ...

  3. r语言c函数怎么用,R语言学习笔记——C#中如何使用R语言setwd()函数

    在R语言编译器中,设置当前工作文件夹可以用setwd()函数. > setwd("e://桌面//") > setwd("e:\桌面\") > ...

  4. R语言学习笔记 07 Probit、Logistic回归

    R语言学习笔记 文章目录 R语言学习笔记 probit回归 factor()和as.factor() relevel() 案例11.4复刻 glm函数 整理变量 回归:Logistic和Probit- ...

  5. R语言学习笔记——入门篇:第一章-R语言介绍

    R语言 R语言学习笔记--入门篇:第一章-R语言介绍 文章目录 R语言 一.R语言简介 1.1.R语言的应用方向 1.2.R语言的特点 二.R软件的安装 2.1.Windows/Mac 2.2.Lin ...

  6. R语言学习笔记——入门篇:第三章-图形初阶

    R语言 R语言学习笔记--入门篇:第三章-图形初阶 文章目录 R语言 一.使用图形 1.1.基础绘图函数:plot( ) 1.2.图形控制函数:dev( ) 补充--直方图函数:hist( ) 补充- ...

  7. R语言学习笔记 06 岭回归、lasso回归

    R语言学习笔记 文章目录 R语言学习笔记 比较lm.ridge和glmnet函数 画岭迹图 图6-4 <统计学习导论 基于R语言的应用>P182 图6-6<统计学习导论 基于R语言的 ...

  8. R语言学习笔记(八)--读写文件与网络爬虫

    R语言学习笔记(八) 1 工作路径 2 保存R对象 3 Scan函数 3-1 从控制台读取数据 3-2 从txt文件读取数据 3-3 从url读取数据 4 按行读写文本文件 5 读取文本文件(txt. ...

  9. R语言学习笔记(三)多元数据的数据特征、相关分析与图形表示

    文章目录 写在前面 独立性检验 χ2\chi^2χ2独立性检验 Fisher独立性检验 Cochran-Mantel-Haenszel χ2\chi^2χ2独立性检验 相关性分析 相关性检验 相关性检 ...

  10. R语言学习笔记(二)处理函数与基本图形绘制

    写在前面 主要内容 数学与统计函数 数据的标准化 概率函数 生成服从正态分布的伪随机数 字符串处理函数 应用于矩阵和数据框的函数 图形绘制 直方图的绘制(hist) 核密度图的绘制 箱线图 经验分布图 ...

最新文章

  1. 学生科技周的讲座-2021-内容准备
  2. check_ping.py
  3. javascript简单介绍
  4. java finalize方法的使用
  5. 无重复字符串的最长子串
  6. java反显是什么_卡西欧反显是什么意思
  7. SQL Server性能调优:资源管理之内存管理篇(上)
  8. 简单的php cms,30个很棒的PHP开源CMS内容管理系统
  9. Android请求麦克风权限+系统权限页面回调
  10. 应届毕业生怎么快速找到高薪工作?
  11. 有哪些IT初学者(新人)成长为技术大牛的真实经历?
  12. Tableau豆瓣电影数据项目实战练习2
  13. 中秋赏月地图出炉,跟随锦江之星去赏月
  14. NLP(词向量、word2vec和word embedding)
  15. mysql中的四大运算符种类汇总20多项,用了三天三夜来整理的,还不赶快收藏
  16. 在windows平台搭建基于nginx的直播服务器
  17. 数学分析模型(一):数据的无量纲处理方法及示例(附完整代码)
  18. 为什么初等矩阵左乘相当于行变换,右乘相当于列变换
  19. 中英离线翻译mac_Translatium for mac(翻译工具)
  20. c语言主函数调用函数

热门文章

  1. 原生Array.isArray()判断对象是否为数组
  2. ERPS环网络端口角色
  3. 【安全测试学习】基于bWAPP靶场,使用sqlmap实现脱库实战
  4. 第2章 系统信息(System Information)
  5. mockito的入门与使用
  6. Protues仿真12864的学习笔记
  7. 计算机更新系统d盘东西还在吗,重装系统其他盘的东西还在吗_重做系统其它盘东西会丢失吗-win7之家...
  8. 理性解析东数西算:“电”在哪里,“算”就在哪里?
  9. 宁波栎社机场停车场怎么收费,栎社机场停车场收费标准
  10. 前端跨域请求及解决方案