[R] bootstrap
arun
smartpink111 at yahoo.com
Wed May 29 20:13:32 CEST 2013
Hi,
I am not sure if this is what you wanted.
set.seed(125)
simdata<- replicate(S,generate(500))
set.seed(125)
data1<- generate(500)
data2<- replicate(S,as.data.frame(t(sample(as.data.frame(t(data1)),100,replace=TRUE))))
dim(data2)
#[1] 12 1000
data2[1:5,1:5]
# [,1] [,2] [,3] [,4] [,5]
#x1 Numeric,100 Numeric,100 Numeric,100 Numeric,100 Numeric,100
#x2 Numeric,100 Numeric,100 Numeric,100 Numeric,100 Numeric,100
#x3 Numeric,100 Numeric,100 Numeric,100 Numeric,100 Numeric,100
#x4 Numeric,100 Numeric,100 Numeric,100 Numeric,100 Numeric,100
#x5 Numeric,100 Numeric,100 Numeric,100 Numeric,100 Numeric,100
A.K.
________________________________
From: Angela Fel Padecio <solewolf301 at yahoo.com>
To: arun <smartpink111 at yahoo.com>
Sent: Wednesday, May 29, 2013 11:05 AM
Subject: Re: bootstrap
thanks for the reply. i think its nearer on the desired result. the previous syntax is this:
set.seed(12345)
S=1000
generate <- function(size) {
x1 <- rnorm(size, mean=0, sd=1)
x2 <- rnorm(size, mean=0, sd=1)
x3 <- rnorm(size, mean=0, sd=1)
x4 <- rnorm(size, mean=0, sd=1)
x5 <- rnorm(size, mean=0, sd=1)
x6 <- rnorm(size, mean=0, sd=1)
x7 <- rnorm(size, mean=0, sd=1)
x8 <- rnorm(size, mean=0, sd=1)
x9 <- rnorm(size, mean=0, sd=1)
x10 <- rnorm(size, mean=0, sd=1)
e<-rnorm(size, mean=0, sd=1)
t_trueps <- (1 + exp( -(b0 + b1*x1 + b2*x2 + b3*x3 + b4*x4 + b5*x5 + b6*x6 + b7*x7
+ b2*x2*x2 + b4*x4*x4 + b7*x7*x7 + b1*0.5*x1*x3 + b2*0.7*x2*x4 +b3*0.5*x3*x5
+ b4*0.7*x4*x6 + b5*0.5*x5*x7 + b1*0.5*x1*x6 + b2*0.7*x2*x3 + b3*0.5*x3*x4
+ b4*0.5*x4*x5 + b5*0.5*x5*x6) ) )^-1
prob.exposure <- runif(size)
t <- ifelse(t_trueps > prob.exposure, 1, 0)
y <- a0 + a1*x1 + a2*x2 + a3*x3 + a4*x4 +a5*x8 + a6*x9 + a7*x10 + g1*t + e
sim <- as.data.frame(cbind(x1, x2, x3 ,x4, x5, x6, x7, x8, x9, x10, t, y))
return(sim)
}
b0 <- 0.05
b1 <- 0.95
b2 <- -0.25
b3 <- 0.6
b4 <- -0.4
b5 <- -0.8
b6 <- -0.5
b7 <- 0.7
a0 <- -3.85
a1 <- 0.3
a2 <- -0.36
a3 <- -0.73
a4 <- -0.2
a5 <- 0.71
a6 <- -0.19
a7 <- 0.26
g1 <- -0.4
simdata <- replicate(S, generate(500))
with a partial result of
[,1] [,2] [,3] [,4] [,5] [,6]
x1 Numeric,500 Numeric,500 Numeric,500 Numeric,500 Numeric,500 Numeric,500
x2 Numeric,500 Numeric,500 Numeric,500 Numeric,500 Numeric,500 Numeric,500
x3 Numeric,500 Numeric,500 Numeric,500 Numeric,500 Numeric,500 Numeric,500
x4 Numeric,500 Numeric,500 Numeric,500 Numeric,500 Numeric,500 Numeric,500
x5 Numeric,500 Numeric,500 Numeric,500 Numeric,500 Numeric,500 Numeric,500
x6 Numeric,500 Numeric,500 Numeric,500 Numeric,500 Numeric,500 Numeric,500
x7 Numeric,500 Numeric,500 Numeric,500 Numeric,500 Numeric,500 Numeric,500
x8 Numeric,500 Numeric,500 Numeric,500 Numeric,500 Numeric,500 Numeric,500
x9 Numeric,500 Numeric,500 Numeric,500 Numeric,500 Numeric,500 Numeric,500
x10 Numeric,500 Numeric,500 Numeric,500 Numeric,500 Numeric,500 Numeric,500
t Numeric,500 Numeric,500 Numeric,500 Numeric,500 Numeric,500 Numeric,500
y Numeric,500 Numeric,500 Numeric,500 Numeric,500 Numeric,500 Numeric,500
up to
[,997] [,998] [,999] [,1000]
x1 Numeric,500 Numeric,500 Numeric,500 Numeric,500
x2 Numeric,500 Numeric,500 Numeric,500 Numeric,500
x3 Numeric,500 Numeric,500 Numeric,500 Numeric,500
x4 Numeric,500 Numeric,500 Numeric,500 Numeric,500
x5 Numeric,500 Numeric,500 Numeric,500 Numeric,500
x6 Numeric,500 Numeric,500 Numeric,500 Numeric,500
x7 Numeric,500 Numeric,500 Numeric,500 Numeric,500
x8 Numeric,500 Numeric,500 Numeric,500 Numeric,500
x9 Numeric,500 Numeric,500 Numeric,500 Numeric,500
x10 Numeric,500 Numeric,500 Numeric,500 Numeric,500
t Numeric,500 Numeric,500 Numeric,500 Numeric,500
y Numeric,500 Numeric,500 Numeric,500 Numeric,500
>
i want a similar structure of this result but not usingreplicate command but bootstrapping or sample command. i simulate n=500 and from this, i get for instance n=100, with 1000 replications. how can i have this? thanks for the help. i appreciate it a lot coz i need it in school. please help. thanks!
________________________________
From: arun <smartpink111 at yahoo.com>
To: Angela Fel Padecio <solewolf301 at yahoo.com>
Cc: R help <r-help at r-project.org>; Bert Gunter <gunter.berton at gene.com>
Sent: Wednesday, May 29, 2013 10:47 PM
Subject: Re: bootstrap
Hi,
Don't know if this helps:
set.seed(244)
data1<- generate(10)
data1
x1 x2 x3 x4 x5 x6
1 -0.89558948 -0.5609722069 -1.0782688 -1.2461548 -0.58857050 1.5708187
2 -0.82379187 -0.9056306209 -1.5700755 -0.6045090 -0.19683863 -0.8969354
3 -1.04303377 -0.2379109244 -0.6589381 -0.7521561 1.37492952 -2.7515409
4 0.00783492 0.4249100106 -0.8221292 -0.6220925 0.91199682 0.6365096
5 0.72986110 0.5355575043 1.7584239 1.4474157 -0.46647958 -0.3799255
6 -1.83691611 1.4025663889
-1.0857996 1.3311993 -2.32091759 0.5866616
7 0.10720511 0.6393573943 -0.2811223 2.2800918 0.09360948 0.9441684
8 -0.36297232 0.6974249024 0.9024777 2.0952564 0.22731336 0.7809357
9 -1.28146538 0.0002775273 1.5413933 -3.2237494 -1.31165562 0.6511996
10 -0.65215069 1.8909461951 -0.7355757 0.3532028 0.05926608 1.6003263
x7 x8 x9 x10 t y
1 -1.24153055 0.1660665 -0.1791897 0.9132159 0 -1.437598
2 0.76953162 0.6579521 -0.2985585 -0.4716139 0 -2.474706
3 -0.70071814 1.4526669 0.4641041 -1.7046221 1
-0.182020
4 0.70752895 -0.1725368 0.8811518 -0.8023556 0 -3.658734
5 -0.02086093 2.1676000 1.8696969 0.4655824 0 -5.059184
6 -0.39706026 1.4280016 0.6295243 -2.2591846 1 -3.229792
7 -0.47859335 1.9391822 1.1545306 -0.3825239 0 -3.989109
8 -0.96075661 2.4411561 -1.9833116 -0.2589653 0 -1.998027
9 -2.82823504 0.1234223 1.1404231 1.3262285 0 -5.323757
10 0.37175204 -1.4488875 -0.2680561 -2.2540178 0 -5.818188
res1<-as.data.frame(t(sample(as.data.frame(t(data1)),20,replace=TRUE)))
row.names(res1)<-1:nrow(res1)
head(res1,2)
# x1 x2 x3 x4
x5 x6 x7
#1 0.00783492 0.424910 -0.8221292 -0.6220925 0.91199682 0.6365096 0.707529
#2 -0.65215069 1.890946 -0.7355757 0.3532028 0.05926608 1.6003263 0.371752
# x8 x9 x10 t y
#1 -0.1725368 0.8811518 -0.8023556 0 -3.658734
#2 -1.4488875 -0.2680561 -2.2540178 0 -5.818188
A.K.
________________________________
From: Angela Fel Padecio <solewolf301 at yahoo.com>
To: arun <smartpink111 at yahoo.com>
Sent: Wednesday, May 29, 2013 10:33 AM
Subject: Re: bootstrap
hi.
thanks for the reply. generally, the command for bootstrapping in R is
sample(x, size, replace=T)
however, i want my x to be the vectors x1 to x10, t and y. i can't do it because i this is what i get:
for instance,
simdata<-sample(data, 20, replace=T)
simdata
x6 x7 x8 x10 x9 x8.1
1 -1.53816891 1.2408110 0.06074567 0.98045523 1.86121959 0.06074567
2 1.33138543 -1.7287688 0.21719209 -0.03431542 0.69361696 0.21719209
3 0.50121104 -2.1667913 0.29435635 2.75808453 -0.07115435 0.29435635
4
-0.87952116 0.8968212 -1.10899986 0.02983622 0.57830958 -1.10899986
5 -0.03325816 -0.2172203 -1.23341708 -0.71446145 0.91041824 -1.23341708
x2 x3 t x3.1 x1 x1.1 t.1 x7.1
1 1.971549999 0.7930340 1 0.7930340 1.4209575 1.4209575 1 1.2408110
2 -0.513132396 2.3926484 0 2.3926484 -0.7346689 -0.7346689 0 -1.7287688
3 0.408742759 1.2250794 0 1.2250794 -1.0200933 -1.0200933 0 -2.1667913
4 -0.048902349 1.2637351 1 1.2637351 -2.3791083 -2.3791083 1 0.8968212
5 0.008808758 0.2171343 1 0.2171343 1.8278435 1.8278435
1 -0.2172203
x10.1 x5 x5.1 x2.1 x10.2 x3.2
1 0.98045523 0.8283620 0.8283620 1.971549999 0.98045523 0.7930340
2 -0.03431542 -0.3461855 -0.3461855 -0.513132396 -0.03431542 2.3926484
3 2.75808453 -0.4661471 -0.4661471 0.408742759 2.75808453 1.2250794
4 0.02983622 0.4343037 0.4343037 -0.048902349 0.02983622 1.2637351
5 -0.71446145 0.9696091 0.9696091 0.008808758 -0.71446145 0.2171343
________________________________
From: arun <smartpink111 at yahoo.com>
To:
Angela Fel Padecio <solewolf301 at yahoo.com>
Cc: R help <r-help at r-project.org>
Sent: Wednesday, May 29, 2013 9:15 PM
Subject: Re: bootstrap
Hi,
You might need to check library(boot). I have never used that before. So, I can't comment much. It is better to post on R-help list. I had seen your postings on Nabble in the past. Unfortunately those postings were not accepted in R-help. You have to directly post at r-help at r-project.org after registering at:
https://stat.ethz.ch/mailman/listinfo/r-help
________________________________
From:
Angela Fel Padecio <solewolf301 at yahoo.com>
To: arun <smartpink111 at yahoo.com>
Sent: Wednesday, May 29, 2013 9:05 AM
Subject: bootstrap
hi. i have these dataset:
set.seed(12345)
S=1000
generate <- function(size) {
x1 <- rnorm(size, mean=0, sd=1)
x2 <- rnorm(size, mean=0, sd=1)
x3 <- rnorm(size, mean=0, sd=1)
x4 <- rnorm(size, mean=0, sd=1)
x5 <- rnorm(size, mean=0, sd=1)
x6 <-
rnorm(size, mean=0, sd=1)
x7 <- rnorm(size, mean=0, sd=1)
x8 <- rnorm(size, mean=0, sd=1)
x9 <- rnorm(size, mean=0, sd=1)
x10 <- rnorm(size, mean=0, sd=1)
e<-rnorm(size, mean=0, sd=1)
t_trueps <- (1 + exp(
-(b0 + b1*x1 + b2*x2 + b3*x3 + b4*x4 + b5*x5 + b6*x6 + b7*x7
+ b2*x2*x2 + b4*x4*x4 + b7*x7*x7 + b1*0.5*x1*x3 + b2*0.7*x2*x4 +b3*0.5*x3*x5
+ b4*0.7*x4*x6 + b5*0.5*x5*x7 + b1*0.5*x1*x6 + b2*0.7*x2*x3 + b3*0.5*x3*x4
+ b4*0.5*x4*x5 + b5*0.5*x5*x6) ) )^-1
prob.exposure <- runif(size)
t <- ifelse(t_trueps > prob.exposure, 1, 0)
y <- a0 + a1*x1 + a2*x2 + a3*x3 + a4*x4 +a5*x8 + a6*x9 + a7*x10 + g1*t + e
sim <- as.data.frame(cbind(x1, x2, x3 ,x4, x5, x6, x7, x8, x9, x10, t, y))
return(sim)
}
b0 <- 0.05
b1 <- 0.95
b2 <- -0.25
b3 <- 0.6
b4
<- -0.4
b5 <- -0.8
b6 <- -0.5
b7 <- 0.7
a0 <- -3.85
a1 <- 0.3
a2 <- -0.36
a3 <- -0.73
a4 <- -0.2
a5 <- 0.71
a6 <- -0.19
a7 <-
0.26
g1 <- -0.4
How can i have a bootstrap sample of 5 from size 10 with 1000 replications, i.e,
data<-generate(10)
where entire row is chosen. for example, if i have this result:
> data<-generate(10)
> data
x1 x2 x3 x4 x5 x6
1 0.9374775 -2.26118020 -1.7502526 -0.04386488 0.6760577 0.5125265
2 -1.3379878 0.54495810 -1.1884175 -0.25142873 -0.7389659
-2.7850113
3 -0.5633375 -0.59191774 -1.4772120 0.74107022 -0.2862497 0.8067138
4 0.3050390 -0.40174654 0.2980965 0.19234445 1.0762513 0.1604186
5 0.6919353 -0.13762332 -0.5352159
0.69566392 1.3655018 -0.8463535
6 0.2988476 1.15431908 -0.1172911 1.38397105 0.2631057 1.7943873
7 -0.1424191 -0.06971174 -1.2759867 -1.25774074 1.2320712 1.7419884
8 1.1835778 -0.06363542 -0.4772019 -0.46848004 1.0064428 -0.6327513
9 0.1942304 -0.79500297 -1.1120049 0.60023523 0.2210129 -1.7187271
10 -2.3568380 0.34227984 -1.0006803 -0.09190608 0.1187722 -0.5756797
x7 x8 x9
x10 t y
1 0.7249311 0.2420102 2.51106406 1.6699921 0 -2.019059
2 -0.5209441 0.4935232
-0.31236000 -0.7717535 1 -2.286814
3 -1.0086591 0.6120889 -0.19684965 0.3355901 0 -2.785737
4 -0.4003396 0.9890145 -2.10427706 0.5364553 1 -2.782166
5 -0.1662400 -0.1357740 0.80220396 -0.3509522 0 -2.424431
6 2.1162941 1.4961478 0.02505993 0.8154658 1 -6.140725
7 0.9965705 1.0531386 0.53474097 -0.1415918 0 -2.922637
8 -0.1959067 -0.3507144 1.15341204 0.6018451 1 -2.602453
9 1.0153402 -0.2612589 -0.70037063 0.4304613 1 -2.788079
10 0.5378466 0.8504346 1.66384979 -1.2695204 0
-3.749164
entire entries of row 1,3,5,7,9 for instance are chosen with 1000. your help is highly appreciated.
More information about the R-help
mailing list