Re: [R] bootstrap help

2010-01-06 Thread luciferyan

I have a similar question.
I want to generate list of 50 bootstrap samples,
it can be done by:

lapply(1:50,function(i){data[sample(nrow(data),size=nrow(data),replace=TRUE),]})

From these  bootstrap samples, I want to work out the Bootstrap estimates,
which is E[y]/E[x].
How can I do it?

' data-data.frame(x=rnorm(49), y=rnorm(49))
 t(sapply(1:50,function(i){colMeans(data[sample(nrow(data),size=nrow(data),replace=TRUE),])}))'
These codes will generate mean of column x and mean of column y. 
Is y the corresponding of x? because x and y are pair data.

Thank you.
-- 
View this message in context: 
http://n4.nabble.com/bootstrap-help-tp949807p1008227.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] bootstrap help

2010-01-06 Thread Dennis Murphy
Hi:

Does this work?

df - data.frame(x = rnorm(49, mean = 1), y = rnorm(49, mean = 2))

# Generate matrix of bootstrap indices
idx - matrix(sample(1:nrow(df), 50 * nrow(df), replace = TRUE),
  nrow = nrow(df))
dim(idx)
# [1] 49 50

# Generate the corresponding x, y, bootstrap samples by using
# matrix indexing by mapping the x and y values to the indices in
# the idx matrix
xboot - matrix(df$x[idx], nrow = nrow(df))
yboot - matrix(df$y[idx], nrow = nrow(df))

# Find respective x, y means from bootstrap samples
xmean.boot - colMeans(xboot)
ymean.boot - colMeans(yboot)

# Take the ratio of the means
xyratio.boot - ymean.boot/xmean.boot

# Now, get bootstrap samples of paired differences
diffboot - yboot - xboot

# Mean differences
diffmean.boot - colMeans(diffboot)

The distribution of the ratio of means is right skewed, whereas the
distribution of the
mean differences looks approximately normal (as one would expect).

Look ma, no [explicit] loops :)  More seriously, behold and appreciate the
power of R's (and S's)
indexing capabilities.

HTH,
Dennis

On Wed, Jan 6, 2010 at 9:42 AM, luciferyan anniehyh...@googlemail.comwrote:


 I have a similar question.
 I want to generate list of 50 bootstrap samples,
 it can be done by:


 lapply(1:50,function(i){data[sample(nrow(data),size=nrow(data),replace=TRUE),]})

 From these  bootstrap samples, I want to work out the Bootstrap estimates,
 which is E[y]/E[x].
 How can I do it?

 ' data-data.frame(x=rnorm(49), y=rnorm(49))
 
 t(sapply(1:50,function(i){colMeans(data[sample(nrow(data),size=nrow(data),replace=TRUE),])}))'
 These codes will generate mean of column x and mean of column y.
 Is y the corresponding of x? because x and y are pair data.

 Thank you.
 --
 View this message in context:
 http://n4.nabble.com/bootstrap-help-tp949807p1008227.html
 Sent from the R help mailing list archive at Nabble.com.

 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide
 http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.


[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] bootstrap help

2009-12-07 Thread Marek Janad
If I read your question now, I think, that I didn't understand you
correctly, but...

My code generates 50 bootstrap samples (50 tables) and for each
calculates mean of column x and mean of column y, so the final result
is matrix with means.

If you don't want to calculate means, code below generates list of 50
bootstrap samples.

lapply(1:50,function(i){data[sample(nrow(data),size=nrow(data),replace=TRUE),]})

Please check help if some functions aren't understandable.

2009/12/7 casperyc caspe...@hotmail.co.uk:

 Hi there,

 This seems to 'simple' for that. ( I mean the codes are too good)
 Can I ask for a bit explaination? or some simplier (maybe few more line
 codes) to archieve that goal?

 And the codes you gave here,
 first line give a table with x and y , random normal values,
 but  the second line, what does that do?

 I totally can't follow that line.
 the result is a 50 by 2 table.
 what is that? is a sample?

 Thanks.

 casper




 Marek Janad wrote:

 data-data.frame(x=rnorm(49), y=rnorm(49))
 t(sapply(1:50,function(i){colMeans(data[sample(nrow(data),size=nrow(data),replace=TRUE),])}))


 r-help@r-project.org

 2009/12/7 casperyc caspe...@hotmail.co.uk:

 Hi there,

 i think that's not what i was aiming for...

 i was aked to

 Generate 50 Bootstrap samples and corresponding estimates

 if i do data[sample(nrow(data),size=50,replace=TRUE),]
 it will give me a table of 50 rows ( 50 sets of x and y)
 then how do i estimate the mean? ( mean was esitmated by the
 bar{x}/bar{y} )

 i mis undertood that too.
 but now, i think what i actually need is

 50 sets of this kind (49 rows) of tables from bootstrapping
 so that i can have 50 different 'bar{x}/bar{y}' s
 then to estimate the mean...

 Thanks.

 casper



 Ben Bolker wrote:

 casperyc casperyc at hotmail.co.uk writes:

 I have 49 pairs in my data.frame 'data'

 x    y
 76   80
 ...  ...

 how do I get a bootstrap sample of size n=50?


 data[sample(nrow(data),size=50,replace=TRUE),]

 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide
 http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.



 --
 View this message in context:
 http://n4.nabble.com/bootstrap-help-tp949807p954053.html
 Sent from the R help mailing list archive at Nabble.com.

 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide
 http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.




 --
 Marek

 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide
 http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.



 --
 View this message in context: 
 http://n4.nabble.com/bootstrap-help-tp949807p954088.html
 Sent from the R help mailing list archive at Nabble.com.

 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.




-- 
Marek

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] bootstrap help

2009-12-06 Thread casperyc

Hi,

I have 49 pairs in my data.frame 'data'

x   y
76  80
138 143
67  67
29  50
381 464
23  48
37  63
120 115

...  ...

how do I get a bootstrap sample of size n=50?

i have tried 
sample(data,size=50,replace=TRUE)
and
sample(data,replace=TRUE)

both didnt seem to work (the latter one only return me with 49 sample)

Thanks.

casper

-- 
View this message in context: 
http://n4.nabble.com/bootstrap-help-tp949807p949807.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] bootstrap help

2009-12-06 Thread Ben Bolker
casperyc casperyc at hotmail.co.uk writes:

 I have 49 pairs in my data.frame 'data'
 
 x y
 7680
 ...  ...
 
 how do I get a bootstrap sample of size n=50?
 

data[sample(nrow(data),size=50,replace=TRUE),]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] bootstrap help

2009-12-06 Thread casperyc

Hi there,

i think that's not what i was aiming for...

i was aked to 

Generate 50 Bootstrap samples and corresponding estimates

if i do data[sample(nrow(data),size=50,replace=TRUE),]
it will give me a table of 50 rows ( 50 sets of x and y)
then how do i estimate the mean? ( mean was esitmated by the bar{x}/bar{y} )

i mis undertood that too.
but now, i think what i actually need is

50 sets of this kind (49 rows) of tables from bootstrapping
so that i can have 50 different 'bar{x}/bar{y}' s
then to estimate the mean...

Thanks.

casper



Ben Bolker wrote:
 
 casperyc casperyc at hotmail.co.uk writes:
 
 I have 49 pairs in my data.frame 'data'
 
 xy
 76   80
 ...  ...
 
 how do I get a bootstrap sample of size n=50?
 
 
 data[sample(nrow(data),size=50,replace=TRUE),]
 
 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide
 http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.
 
 

-- 
View this message in context: 
http://n4.nabble.com/bootstrap-help-tp949807p954053.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] bootstrap help

2009-12-06 Thread Marek Janad
data-data.frame(x=rnorm(49), y=rnorm(49))
t(sapply(1:50,function(i){colMeans(data[sample(nrow(data),size=nrow(data),replace=TRUE),])}))


r-help@r-project.org

2009/12/7 casperyc caspe...@hotmail.co.uk:

 Hi there,

 i think that's not what i was aiming for...

 i was aked to

 Generate 50 Bootstrap samples and corresponding estimates

 if i do data[sample(nrow(data),size=50,replace=TRUE),]
 it will give me a table of 50 rows ( 50 sets of x and y)
 then how do i estimate the mean? ( mean was esitmated by the bar{x}/bar{y} )

 i mis undertood that too.
 but now, i think what i actually need is

 50 sets of this kind (49 rows) of tables from bootstrapping
 so that i can have 50 different 'bar{x}/bar{y}' s
 then to estimate the mean...

 Thanks.

 casper



 Ben Bolker wrote:

 casperyc casperyc at hotmail.co.uk writes:

 I have 49 pairs in my data.frame 'data'

 x    y
 76   80
 ...  ...

 how do I get a bootstrap sample of size n=50?


 data[sample(nrow(data),size=50,replace=TRUE),]

 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide
 http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.



 --
 View this message in context: 
 http://n4.nabble.com/bootstrap-help-tp949807p954053.html
 Sent from the R help mailing list archive at Nabble.com.

 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.




-- 
Marek

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] bootstrap help

2009-12-06 Thread Casper
Hi,

I have a dataframe with x and y in pairs, ( 49 pairs i.e. 49 rows )

x y
76 80
138 143
67 67
29 50
381 464
23 48
37 63
120 115


how do i generate 50 Bootstrap samples and corresponding estimates?

i have tried

sample( data, size=50,replace=T)

does not seem to be right

Thanks.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] bootstrap help

2009-12-06 Thread casperyc

Hi there,

This seems to 'simple' for that. ( I mean the codes are too good)
Can I ask for a bit explaination? or some simplier (maybe few more line
codes) to archieve that goal?

And the codes you gave here,
first line give a table with x and y , random normal values,
but  the second line, what does that do?

I totally can't follow that line.
the result is a 50 by 2 table.
what is that? is a sample? 

Thanks.

casper




Marek Janad wrote:
 
 data-data.frame(x=rnorm(49), y=rnorm(49))
 t(sapply(1:50,function(i){colMeans(data[sample(nrow(data),size=nrow(data),replace=TRUE),])}))
 
 
 r-help@r-project.org
 
 2009/12/7 casperyc caspe...@hotmail.co.uk:

 Hi there,

 i think that's not what i was aiming for...

 i was aked to

 Generate 50 Bootstrap samples and corresponding estimates

 if i do data[sample(nrow(data),size=50,replace=TRUE),]
 it will give me a table of 50 rows ( 50 sets of x and y)
 then how do i estimate the mean? ( mean was esitmated by the
 bar{x}/bar{y} )

 i mis undertood that too.
 but now, i think what i actually need is

 50 sets of this kind (49 rows) of tables from bootstrapping
 so that i can have 50 different 'bar{x}/bar{y}' s
 then to estimate the mean...

 Thanks.

 casper



 Ben Bolker wrote:

 casperyc casperyc at hotmail.co.uk writes:

 I have 49 pairs in my data.frame 'data'

 x    y
 76   80
 ...  ...

 how do I get a bootstrap sample of size n=50?


 data[sample(nrow(data),size=50,replace=TRUE),]

 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide
 http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.



 --
 View this message in context:
 http://n4.nabble.com/bootstrap-help-tp949807p954053.html
 Sent from the R help mailing list archive at Nabble.com.

 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide
 http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.

 
 
 
 -- 
 Marek
 
 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide
 http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.
 
 

-- 
View this message in context: 
http://n4.nabble.com/bootstrap-help-tp949807p954088.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.