📄 s examples for simple bootstraps.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0066)http://www-stat.stanford.edu/~susan/courses/b494/index/node54.html -->
<!--Converted with LaTeX2HTML 2K.1beta (1.57)original version by: Nikos Drakos, CBLU, University of Leeds* revised and updated by: Marcus Hennecke, Ross Moore, Herb Swan* with significant contributions from: Jens Lippmann, Marek Rouchal, Martin Wilck and others --><HTML><HEAD><TITLE>S examples for simple bootstraps</TITLE>
<META content="S examples for simple bootstraps" name=description>
<META content=t name=keywords>
<META content=document name=resource-type>
<META content=global name=distribution>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2900.2523" name=GENERATOR>
<META http-equiv=Content-Style-Type content=text/css><LINK
href="S examples for simple bootstraps.files/t.css" rel=STYLESHEET><LINK
href="node55.html" rel=next><LINK href="node53.html" rel=previous><LINK
href="node52.html" rel=up><LINK href="node55.html" rel=next></HEAD>
<BODY><!--Navigation Panel--><A
href="http://www-stat.stanford.edu/~susan/courses/b494/index/node55.html"
name=tex2html911><IMG height=24 alt=next
src="S examples for simple bootstraps.files/next.png" width=37 align=bottom
border=0></A> <A
href="http://www-stat.stanford.edu/~susan/courses/b494/index/node52.html"
name=tex2html907><IMG height=24 alt=up
src="S examples for simple bootstraps.files/up.png" width=26 align=bottom
border=0></A> <A
href="http://www-stat.stanford.edu/~susan/courses/b494/index/node53.html"
name=tex2html901><IMG height=24 alt=previous
src="S examples for simple bootstraps.files/prev.png" width=63 align=bottom
border=0></A> <A
href="http://www-stat.stanford.edu/~susan/courses/b494/index/node124.html"
name=tex2html909><IMG height=24 alt=index
src="S examples for simple bootstraps.files/index.png" width=43 align=bottom
border=0></A> <BR><B>Next:</B> <A
href="http://www-stat.stanford.edu/~susan/courses/b494/index/node55.html"
name=tex2html912>Parametric bootstrap</A> <B>Up:</B> <A
href="http://www-stat.stanford.edu/~susan/courses/b494/index/node52.html"
name=tex2html908>The bootstrap : the</A> <B>Previous:</B> <A
href="http://www-stat.stanford.edu/~susan/courses/b494/index/node53.html"
name=tex2html902>In practice</A> <B><A
href="http://www-stat.stanford.edu/~susan/courses/b494/index/node124.html"
name=tex2html910>Index</A></B> <BR><BR><!--End of Navigation Panel-->
<H2><A name=SECTION00312000000000000000>S examples for simple bootstraps</A>
</H2><PRE>laws<-scan()
576 3.39
635 3.30
558 2.81
578 3.03
666 3.44
580 3.07
555 3.00
661 3.43
651 3.36
605 3.13
653 3.12
575 2.74
545 2.76
572 2.88
594 2.96
"boot.simple"<-
function(data, fun, nboot, ...)
{
n <- nrow(as.matrix(data))
res.boot <- vector("list", length = nboot)
indb <- matrix(sample(n, size = n * nboot, T), nrow = nboot)
for(b in (1:nboot)) {
res <- fun(indb[b, ], ...)
plot(res, xlab = "", ylab = "", lab = c(0, 0, 0))
res.boot[[b]] <- res
}
return(res.boot)
}
"boot"<-
function(ind, nboot, fun, ...)
{
# Tibshirani's efficient Bootstrap
# If fun is function of a matrix or returns multiple values
# it has to be transformed before
data <- matrix(sample(ind, size = length(ind) * nboot, replace = T),
nrow = nboot)
return(apply(data, 1, fun, ...))
}
#Elementary example of using boot
corr<- function(x, xdata)
{
cor(xdata[x, 1], xdata[x, 2])
}
j1000 <- boot(1:15, 1000, theta, laws)
hist(j1000 - 0.777)
q.bt <- quantile(junk1000, probs = c(0.01, 0.99))
#With greek label
hist(junk1000 - 0.777, nclass = 40, xlab = "q* - q", font = 13)
#Example of Bootstrapping a Mann-Whitney statistic
"mw"<-
function(x, y)
{
#Function that computes a Mann-Whitney statistic for two samples
n <- length(x)
m <- length(y)
rks <- rank(c(x, y))
wx <- sum(rks[1:n])
ux <- n * m + (m * (m + 1))/2 - wx
mwx <- 1 - ux/(m * n)
return(mwx)
}
"mwbt"<-
function(x, y, nboot = 100, fun = mw, ...)
{
bts <- rep(0, nboot)
for(b in (1:nboot)) {
xbts <- sample(x, length(x), replace = T)
ybts <- sample(y, length(y), replace = T)
bts[b] <- fun(xbts, ybts, ...)
}
return(bts)
}
"mwxy"<-
function(xy, n)
{
# n <- length(x)
m <- length(xy) - n
rks <- rank(xy)
wx <- sum(rks[1:n])
ux <- n * m + (m * (m + 1))/2 - wx
mwx <- 1 - ux/(m * n)
return(mwx)
}
</PRE><BR>
<HR>
<ADDRESS>Susan Holmes 2002-01-12 </ADDRESS></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -