[R] error message

2011-01-18 Thread wangwallace

I was running a sampling syntax based on a data frame (ago) of 160 rows and
25 columns. Below are the column names:

 names(ago)
 [1] SubID  AGR1   AGR2   AGR3   AGR4   AGR5   AGR6   AGR7  
AGR8  
[10] AGR9   AGR10  WAGR1  WAGR2  WAGR3  WAGR4  WAGR5  WAGR6 
WAGR7 
[19] WAGR8  WAGR9  WAGR10 ocbi   ocbo   cwbi   cwbo  

 dim(ago)
[1] 160  25

Here below is the syntax:

 crossed1-function(df){  
+ s1-df[sample(nrow(df),16,replace=F),]
+ s2-t(apply(s1[,2:11],1,sample,10))
+ s2-data.frame(s2)
+ s3-df[-s1$SubID,] 
+ ind-t(replicate(nrow(s3),sample(10))) 
+ s4-matrix(nrow=nrow(s3),ncol=10)
+ rownames(s4)-rownames(s3)
+ s4-data.frame(s4) 
+ for(i in seq(nrow(s3))){
+ s4[i,1]-s3[i,1+ind[i,1]]
+ s4[i,2:10]-s3[i,11+ind[i,2:10]]
+ }
+ colnames(s2)-colnames(s4)
+ rbind(s2,s4)[order(as.numeric(rownames(rbind(s2,s4,]
+ }
 result1-vector(list,1000)
 for(i in 1:1000)result1[[i]]-crossed1(ago)

These syntaxes worked out perfectly. I successfully drew 1000 random
samples.
I applied the syntax above to another data set called agr, which basically
has the same column names:

 names(agr)
 [1] SubID  AGR1   AGR2   AGR3   AGR4   AGR5   AGR6   AGR7  
AGR8  
[10] AGR9   AGR10  WAGR1  WAGR2  WAGR3  WAGR4  WAGR5  WAGR6 
WAGR7 
[19] WAGR8  WAGR9  WAGR10 ocbi   ocbo   cwbi   cwbo  

agr and ago are basically the same except that agr has more rows than ago:

 dim(agr)
[1] 300  25

here below is the same syntax I've used for agr:

 crossed1-function(df){  
+ s1-df[sample(nrow(df),30,replace=F),] # note I have changed 16 to 30
+ s2-t(apply(s1[,2:11],1,sample,10))
+ s2-data.frame(s2)
+ s3-df[-s1$SubID,] 
+ ind-t(replicate(nrow(s3),sample(10))) 
+ s4-matrix(nrow=nrow(s3),ncol=10)
+ rownames(s4)-rownames(s3)
+ s4-data.frame(s4) 
+ for(i in seq(nrow(s3))){
+ s4[i,1]-s3[i,1+ind[i,1]]
+ s4[i,2:10]-s3[i,11+ind[i,2:10]]
+ }
+ colnames(s2)-colnames(s4)
+ rbind(s2,s4)[order(as.numeric(rownames(rbind(s2,s4,]
+ }
 result1-vector(list,1000)
 for(i in 1:1000)result1[[i]]-crossed1(agr)
Error in xj[i] : only 0's may be mixed with negative subscripts

However, I got an error message shown above. It is kinda weird. Could
anybody please explain what I've did wrong? Many thanks in advance!! :)
-- 
View this message in context: 
http://r.789695.n4.nabble.com/error-message-tp3223412p3223412.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] error message

2011-01-18 Thread wangwallace

I tried it several times. it still gives me the same error.

I also used traceback()

here is what I got:

 traceback()
3: `[.data.frame`(df, -s1$SubID, )
2: df[-s1$SubID, ]
1: crossed1(agr)

But I don't think there is anything wrong with it..
-- 
View this message in context: 
http://r.789695.n4.nabble.com/error-message-tp3223412p3224006.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] error message

2011-01-18 Thread wangwallace

Peter,

I found it. There is a missing value under column SubID. I filled it out
with an ID number, and the error message never occur again. Although I found
the problem, I have no idea why would this be associated with such an error
message. If anybody else have some input, I would really appreciate it. :)

Regards,
Wallace
-- 
View this message in context: 
http://r.789695.n4.nabble.com/error-message-tp3223412p3224213.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.


[R] how to sort new data frame based on the original data frame

2011-01-11 Thread wangwallace

I have a really simple question

I have a data frame of 8 variables (the first column is the subjects' id): 

SubID G1G2 G3 G4W1W2  W3W4 
  1  6  5   6   2  6  22   4 
  2  6  4   7   2  6  62   3 
  3  5  5   5   5  5  54   5 
  4  5  4   3   4  4  45   2 
  5  5  6   7   5  6  44   1 
  6  5  4   3   6  4  37   3 
  7  3  6   6   3  6  52   1 
  8  3  6   6   3  6  54   7 


this data frame have two sets of variables. each set simply represent one
scale. as shown above, the first scale, say G, consists of four items: G1,
G2, G3, and G4, whereas the second scale, say W, also has four items: W1,
W2, W3, W4. 
the leftmost column lists the subjects' ID. 

I drew 100 new random samples based on the data frame. here is the structure
of each new random sample:
   varvar   var var 
g  g  g   g   
g  g  g   g   
w  w w   w   
w  w w   w   
w  w w   w 
w  w w   w 
w  w w   w 
w  w w   w  

each random sample satisfies the following rules:

###the top two rows have to be filled with 2 random rows of the 8 rows of G
numbers. the rest should be filled with 6 random rows of the 8 rows of W
numbers. At the same time, the SubIDs of all eight rows should be different
among each other.

here below is the syntax I've used:

 fff-function(dat,g=2,w=6){
+ sel1-sample(1:8,g)
+ sel2-sample((1:8)[-sel1],w)
+ M=dat[sel1,2:5]
+ N=dat[sel2,6:9]
+ colnames(N)-colnames(M)
+ rbind(M,N)
+}

 result-vector(list,100)
 for(i in 1:100)result[[i]]-fff(data,2,6)
 result

here is the first random sample:

 result[[1]]

   G1G2 G3 G4
3  5  5   5   5
6  5  4   3   6
4  4  4   5   2  
1  6  2   2   4 
7  6  5   2   1
2  6  6   2   3
5  6  4   4   1
8  6  5   4   7

I am wondering how can I sort the rows of each new random samples in the
same order that is corresponding to the SubID in the original data.
Specifically, what kind of syntax should I've added into the one I've used
above to make the random sample, say, the first random sample, look like:

 result[[1]]

   G1G2 G3 G4
1  6  2   2   4 
2  6  6   2   3
3  5  5   5   5
4  4  4   5   2  
5  6  4   4   1
6  5  4   3   6
7  6  5   2   1
8  6  5   4   7

Many thanks!!
-- 
View this message in context: 
http://r.789695.n4.nabble.com/how-to-sort-new-data-frame-based-on-the-original-data-frame-tp3209353p3209353.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] how to run linear regression models at once

2011-01-10 Thread wangwallace

hey, Dennis,

I applied your syntax into my data frame with different variable names.
Specifically, I replaced y1 and y2, with ocbi and ocbo. I got the following
error message. Could you please explain why would this happen? Again,
thanks!
 
 for(i in 1:5){fm1-as.formula(paste('ocbi',ms[i],sep='~'))
+ fm2-as.formula(paste('ocbo',ms[i],sep'~'))

Error: unexpected string constant in:
for(i in 1:5){fm1-as.formula(paste('ocbi',ms[i],sep='~'))
fm2-as.formula(paste('ocbo',ms[i],sep'~'
-- 
View this message in context: 
http://r.789695.n4.nabble.com/how-to-run-linear-regression-models-at-once-tp3179256p3207726.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] how to run linear regression models at once

2011-01-10 Thread wangwallace

I am too negligent... thank you very much for pointing out that!
-- 
View this message in context: 
http://r.789695.n4.nabble.com/how-to-run-linear-regression-models-at-once-tp3179256p3207823.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] how to run linear regression models at once

2011-01-08 Thread wangwallace

cool, it worked. thank you very much for your help!! :)
-- 
View this message in context: 
http://r.789695.n4.nabble.com/how-to-run-linear-regression-models-at-once-tp3179256p3205186.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.


[R] how to run linear regression models at once

2011-01-07 Thread wangwallace

hey, folks,

I have two very simple questions. I am not quite sure if I can do this using
R. 

so, I am analyzing a large data frame with thousands of variables. For
example:

Dependent variables: d1, d2, d3 (i.e., there are three dependent variables)

Independent variables: s1, s2, s3, ..s1000 (i.e., there are 1000
independent variables)

now I want to run simple linear regression analyses of dependent variables
on independent variables. A laborious way to do this is running 1000 linear
regression models for each dependent variable separately. This would take a
lot of time. My question is: 

1) is there a easy way to run all 1000 linear regression analyses for each
dependent variable at once?  
2) after running all 1000 linear regression analyses at once, how can I
write 3000 regression weights (1000 regression weights for each dependent
variable)  and its significance level in to a file (e.g., csv, excel, ect).

Many thanks in advance!!
-- 
View this message in context: 
http://r.789695.n4.nabble.com/how-to-run-linear-regression-models-at-once-tp3179256p3179256.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] how to apply sample function to each row of a data frame?

2010-11-22 Thread wangwallace

I tried it. it works out perfectly. you save my life.
-- 
View this message in context: 
http://r.789695.n4.nabble.com/Re-how-to-apply-sample-function-to-each-row-of-a-data-frame-tp3050933p3054117.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] how to apply sample function to each row of a data frame?

2010-11-21 Thread wangwallace

it works. I really appreciate the help! :)
-- 
View this message in context: 
http://r.789695.n4.nabble.com/Re-how-to-apply-sample-function-to-each-row-of-a-data-frame-tp3050933p3052098.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.


[R] use sample function to create new data frame

2010-11-21 Thread wangwallace

here is my original data frame

   a   b  c   A  B   C
[1,]  1   2  3   4  5   6
[2,]  7   8  9  10 11 12
[3,] 13 14 15 16 17 18

a, b, c are type I variables
A, B, C are type II variables

how can I create a new data frame in which:

1) in each row, there are one random number from type I variables, and two
random numbers from type II variables

2) meanwhile, in each row, the two type II numbers have to be only those
numbers that are not corresponding to the type I number. For example, if the
type I number is 1, the type II numbers should not include 4. 

the new data frame should be like this:

   [,1] [,2] [,3]
[1,]I IIII
[2,]I IIII
[3,]I IIII

Many thanks!! :)

-- 
View this message in context: 
http://r.789695.n4.nabble.com/use-sample-function-to-create-new-data-frame-tp3052110p3052110.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] use sample function to create new data frame

2010-11-21 Thread wangwallace

Gong, thanks!! but your syntax did work out as I would expect. here following
are the reasons:

each row from my original data frame represents data from one subject. when
the one type I number and the 
two type II numbers are drawn from the original data frame, both type I
number and type II numbers should both from the same subject. by doing so,
the new data frame should only have 3 rows with each representing one
subject. I am trying to revise your syntax to make it work... :)
-- 
View this message in context: 
http://r.789695.n4.nabble.com/use-sample-function-to-create-new-data-frame-tp3052110p3052629.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] how to apply sample function to each row of a data frame?

2010-11-21 Thread wangwallace

Hey, PS,

I revised your syntax and used it to another one of my data frames (df).

here is the data frame:

   a   b  c   A  B   C
[1,]  1   2  3   4  5   6
[2,]  7   8  9  10 11 12
[3,] 13 14 15 16 17 18

a, b, c are type I variables
A, B, C are type II variables 
each row represent the data from one subject

my purpose is to create a new data frame in which:

1) in each row, there are one random number from type I variables, and two
random numbers from type II variables

2) meanwhile, in each row, the two type II numbers have to be only those
numbers that are not corresponding to the type I number. For example, if the
type I number is 1, the type II numbers should not include 4.

3) type I number and type II numbers in each row should be all from the same
subject.

the new data frame should be like this:

   [,1] [,2] [,3]
[1,]I IIII
[2,]I IIII
[3,]I IIII 

Here below are the revised syntax based on yours:

 s4-t(t(apply(df[,1:3],1,sample,1)))
 s5-t(apply(df[,4:6],1,sample,2))
 s6-cbind(s4,s5)

revised syntax works perfect except that it does not satisfy the following
rule I mentioned above:


2) meanwhile, in each row, the two type II numbers have to be only those
numbers that are not corresponding to the type I number. For example, if the
type I number is 1, the type II numbers should not include 4.

Do you know how to revise it to make it work? I tried different methods, but
failed. Could you please help? Again, thanks a million
-- 
View this message in context: 
http://r.789695.n4.nabble.com/Re-how-to-apply-sample-function-to-each-row-of-a-data-frame-tp3050933p3052752.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.


[R] how to apply sample function to each row of a data frame?

2010-11-19 Thread wangwallace

this is a simple question, but I wasn't able to figure it out myself. 

here is the data frame:

M P Q
1 2 3
4 5 6
7 8 9

M, P, Q each represent a variable

I want to draw 2 random sample from each row separately to create a new data
frame. how can I do it?

thanks!!
-- 
View this message in context: 
http://r.789695.n4.nabble.com/how-to-apply-sample-function-to-each-row-of-a-data-frame-tp3050787p3050787.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] how to apply sample function to each row of a data frame?

2010-11-19 Thread wangwallace

 this is a simple question, but I wasn't able to figure it out myself.

 here is the data frame:

 M P Q
 1 2 3
 4 5 6
 7 8 9

 M, P, Q each represent a variable

 I want to draw 2 random sample from each row separately to create a new
 data  frame. how can I do it?

 thanks!! 

sorry for the confusion. let me explain it in more details:

actually, what I meant is to draw two random numbers from each row
separately to create a new data frame. for example, an example output could
be:

1 3
4 5
9 8

The key is these two numbers in each row should be randomized. They don't
have to be drawn from the same column. Specifically, as shown above, two
numbers in the first row belong to columns M and Q, whereas the two numbers
in the second row belong to columns M and P. I am wondering how the two
numbers can be sampled from each row separately. 

Finally, since the column names of the sampled two numbers across these
three rows will probably be different, I guess I cannot use rbind to put all
these three rows together. Is there anything else (I don't want use list) I
can use to align three rows with different column names together? Also, if I
can write a function for it. May I use some syntax like the following to
repeat the whole process 1000 times (i.e., 1000 samples)?

 result-vector(list,1000)
 for(i in 1:1000)result[[i]]-fff(data)#fff(data) is the function name
 result
 
Again, I really appreciate your help. without your help, I couldn't have got
this far. :)
-- 
View this message in context: 
http://r.789695.n4.nabble.com/Re-how-to-apply-sample-function-to-each-row-of-a-data-frame-tp3050933p3051253.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] New Sampling question

2010-11-18 Thread wangwallace

Dear Ista Zahn-2,

If you can give me some advice, I really appreciate it. I have been working
on it for days. it seems hard for some novice of R like me to write flexible
functions myself. 

This is for my dissertation. CSE and WSE are two scales of the same
construct. The sampling strategy I wanted above allows me check how the
items of these two scales vary within person and across person. 

Also, I added another rule: draw 1000 random samples...

Again, Thanks!

Wallace
-- 
View this message in context: 
http://r.789695.n4.nabble.com/New-Sampling-question-tp3047885p3048948.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] New Sampling question

2010-11-18 Thread wangwallace

Also, I need some function at the end which would enable me to draw 1000 such
random samples. thanks! :)
-- 
View this message in context: 
http://r.789695.n4.nabble.com/New-Sampling-question-tp3047885p3048958.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] Odp: Sampling problem

2010-11-18 Thread wangwallace

I tried. yours works too. thanks a bounch!! Man
-- 
View this message in context: 
http://r.789695.n4.nabble.com/Sampling-problem-tp3043804p3049013.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] New Sampling question

2010-11-18 Thread wangwallace

Hi, Mike,

thank you very much!!:)

the following two functions are really helpful, which I didn't even know.
Actually, I searched the forum for something like this, but failed. Now I am
still trying to make up my own functions. :)

sample(a[,2],2) #randomly draw two numbers from a column

sample(a[2,],2) #randomly draw two numbers from a row 
-- 
View this message in context: 
http://r.789695.n4.nabble.com/New-Sampling-question-tp3047885p3049083.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] New Sampling question

2010-11-18 Thread wangwallace


I spent the whole afternoon on it, but there is still no progress. I wish I
could take some courses... :(
-- 
View this message in context: 
http://r.789695.n4.nabble.com/New-Sampling-question-tp3047885p3049796.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.


[R] New Sampling question

2010-11-17 Thread wangwallace

I have another question about drawing samples from a data frame. This might
sound really tricky. Let me use a data frame I have posted earlier as an
example:

SubIDCSE1 CSE2 CSE3 CSE4 WSE1 WSE2 WSE3 WSE4
  1  6  5   6   2  6  22   4
  2  6  4   7   2  6  62   3
  3  5  5   5   5  5  54   5
  4  5  4   3   4  4  45   2
  5  5  6   7   5  6  44   1
  6  5  4   3   6  4  37   3
  7  3  6   6   3  6  52   1
  8  3  6   6   3  6  54   7 

this data frame have two sets of variables. each set simply represent one
scale. as shown above, the first scale, say CSE, consists of four items:
CSE1, CSE2, CSE3, and CSE4, whereas the second scale, say WSE, also has four
items: WSE1, WSE2, WSE3, WSE4.
the leftmost column lists the subjects' ID. 

I wanna create a new data frame through sampling random numbers from the
data frame above. Below is the structure of the new data frame.

SubIDvarvar   var var 
  s  c  c  c   c  
  s  c  c  c   c  
  s  c  w w   w  
  s  c  w w   w  
  s  c  w w   w
  s  c  w w   w
  s  c  w w   w
  s  c  w w   w

in the new data frame:
 
s= SubID range from 1 to 8
var= variables
c=CSE numbers
w=WSE numbers

some rules to construct the new data frame:

1. the top two rows have to be filled with CSE numbers; the numbers in the
cells of each row should be randomized. for example, if the first row is an
array of numbers from subject 4, they can follow the order: 4(CSE2),
5(CSE1), 3(CSE3), and 4(CSE4). Also, the numbers in the second row does not
have to follow the order of the first row. for example, similarly, if the
first row is an array of numbers from subject 4 in the order: 4(CSE2),
5(CSE1), 3(CSE3), and 4(CSE4), numbers in the second row (assuming it is
from subject 8) does not have to be 6(CSE2), 3(CSE1), 6(CSE3), and 3(CSE4).
numbers in these two rows should be drawn without replacement.

2. each of the rest of the rows should include a CSE number in the leftmost
cell and three WSE numbers on the right. At the same time, in each row, the
three WSE numbers on the right have to be only those numbers that are not
corresponding to the CSE number in the leftmost cell. For example, if the
CSE number in the leftmost cell is 4, a CSE2 number from subject 6, the
three WSE numbers on the right side can only be 4(WSE1), 7(WSE3), and
3(WSE4) from subject 6. 

3. the numbers in each row can only be drawn from the same subject. Also,
Subjects should be randomized. Specifically, they does have to be in the
following order:

 SubID
  1 
  2  
  3
  4  
  5  
  6  
  7  
  8
  
they can be:

 SubID
  2 
  8  
  5
  4  
  1  
  6  
  7  
  3

Any ideas?  Thanks in advance!! :)
-- 
View this message in context: 
http://r.789695.n4.nabble.com/New-Sampling-question-tp3047885p3047885.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] Sampling problem

2010-11-16 Thread wangwallace

Michael, I really appreciate your help.

but I got the following error message when I wan trying to run the function
written by you:

Error in out[i, ] - apply(help[, c(grp1 + 1, grp2 + 5)], 2, sample, 1) : 
  number of items to replace is not a multiple of replacement length

I am not quite sure why would this happen.

As a novice of R, these functions are kinda complex for me. I am wondering
if it is doable without using loops like that.

Again, thank you so much!!!  
-- 
View this message in context: 
http://r.789695.n4.nabble.com/Sampling-problem-tp3043804p3044249.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] Sampling problem

2010-11-16 Thread wangwallace

yes, the data.frame is exactly the same as the one I posted earlier. 

I was trying to see if the loop function works. And I got that message. here
below is the syntax I was trying to run, followed by the error message at
the end:
 sampleX-function(X,nGrp1,nsamples){if(nGrp1=4)stop(can't sample all
 group 1 variables)
+ out-matrix(0,nsamples,nGrp1+1)
+ for(i in 1:nsamples){
+ grp1-sample(4,nGrp1)
+ grp2-sample((1:4)[-grp1],)
+ out[i,]-apply(X[,c(grp1+1,grp2+5)],2,sample,1)
+ }
+ out}
 sampleX(help,1,10)
Error in out[i, ] - apply(X[, c(grp1 + 1, grp2 + 5)], 2, sample, 1) : 
  number of items to replace is not a multiple of replacement length

By the way, it is only a small piece of my data set, which has 12 variables
(or columns) for each group (grp1: CSE1, CSE2, CSE3, CSE4, CSE5, CSE6, CSE7,
CSE8, CSE9, CSE10, CSE11, CSE12; grp2: WSE1, WSE2, WSE3, WSE4, WSE5, WSE6,
WSE7, WSE8, WSE9, WSE10, WSE11, WSE12). I will draw 1000 random samples for
each of the 11 different combinations below: 

combination 1: 1 variable from grp1 + 11 variables from grp2 = 12 variables
combination 2: 2 variable from grp1 + 10 variables from grp2 = 12 variables
combination 3: 3 variable from grp1 + 9 variables from grp2 = 12 variables
combination 4: 4 variable from grp1 + 8 variables from grp2 = 12 variables
combination 5: 5 variable from grp1 + 7 variables from grp2 = 12 variables
combination 6: 6 variable from grp1 + 6 variables from grp2 = 12 variables
combination 7: 7 variable from grp1 + 5 variables from grp2 = 12 variables
combination 8: 8 variable from grp1 + 4 variables from grp2 = 12 variables
combination 9: 9 variable from grp1 + 3 variables from grp2 = 12 variables
combination 10: 10 variable from grp1 + 2 variables from grp2 = 12 variables
combination 11: 11 variable from grp1 + 1 variables from grp2 = 12 variables

As shown above, the sum of the variables in each combination will have to be
12. Also, I want to restrict a vector I am going to sample from to only
those columns that are not correspond to grp1 variables I have sampled. For
example, if I sampled 1 variable, say CSE1, from grp1, the other 11
variables from grp2 should not include WSE1; if I sampled 2 variables, say
CSE1 and CSE2, from grp1, the other 10 variables from grp2 should not
include WSE1 and WSE2.

Anyway, this is a lot more complicated example than the one I described in
my first post. But I think I can modify your function if I wanna apply it to
the large data set with 12 variables for each group, since they basically
share the same method. Now I am wondering where the error message is from.

Again, thanks!! :)


-- 
View this message in context: 
http://r.789695.n4.nabble.com/Sampling-problem-tp3043804p3045095.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] Odp: Sampling problem

2010-11-16 Thread wangwallace

Fabulicious! It worked!!! 

One more question, in the following data frame as posted above:

SubIDCSE1 CSE2 CSE3 CSE4 WSE1 WSE2 WSE3 WSE4
  1  6  5   6   2  6  22   4
  2  6  4   7   2  6  62   3
  3  5  5   5   5  5  54   5
  4  5  4   3   4  4  45   2
  5  5  6   7   5  6  44   1
  6  5  4   3   6  4  37   3
  7  3  6   6   3  6  52   1
  8  3  6   6   3  6  54   7 

I want to draw the first random sample consisting of a row of integers under
the first group of variables (CSE1, CSE2, CSE3, CSE4). For example, assuming
the first draw yielded a sample of the first row (6, 5, 6, 2), now I want to
draw another random sample consisting of two rows of integers under the
second group of variables (WSE1, WSE2, WSE3, WSE4). Also, for the second
draw, I want to restrict a vector I am going to sample from to only those
rows that are not correspond to SubID I have sampled. That is, I want to
sample two rows of integers under the second group of variables (WSE1, WSE2,
WSE3, WSE4) from rows 2, 3, 4, 5, 6, 7, and 8.

Also, I want to repeat this whole process (drawing 1 random row of integers
under the first group of variables first, AND then another two random rows
under the second group of variables) for 1000 times. Any ideas? would that
be possible to do it by just revising the syntax you wrote above? many
thanks!!!
-- 
View this message in context: 
http://r.789695.n4.nabble.com/Sampling-problem-tp3043804p3045352.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] Odp: Sampling problem

2010-11-16 Thread wangwallace

I figured it out myself.

Again, Michael and Petr, many thanks to both of you!!! :) 
-- 
View this message in context: 
http://r.789695.n4.nabble.com/Sampling-problem-tp3043804p3046161.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.


[R] Sampling problem

2010-11-15 Thread wangwallace

Hey,

I am hoping someone can help me with a sampling question.

I have a data frame of 8 variables (the first column is the subjects' id):

SubIDCSE1 CSE2 CSE3 CSE4 WSE1 WSE2 WSE3 WSE4 
  1  6  5   6   2  6  22   4
  2  6  4   7   2  6  62   3
  3  5  5   5   5  5  54   5
  4  5  4   3   4  4  45   2
  5  5  6   7   5  6  44   1
  6  5  4   3   6  4  37   3
  7  3  6   6   3  6  52   1
  8  3  6   6   3  6  54   7 

the 6 variables are categorized into two groups with CSE1, CSE2, CSE3, and
CSE4 in one group and the rest in another group. 

sample(data[,2:4],2,replace=FALSE)
 
   CSE1 CSE2 
1  65
2  64   
3  55   
4  54
5  56   
6  54
7  36
8  36

Now I want to sample 1 column from another group of variables (i.e., WSE1,
WSE2, WSE3, WSE4), but I want to restrict a vector I am going to sample from
to only those columns that are not correspond to GROUP 1 variables I have
sampled. That is, I want to sample a column from WSE3, WSE4  Columns  
corresponding to CSE1 and CSE2 (i.e., WSE1, WSE2) need to be dropped. 

How can I do this? what if I want to repeat this whole process (drawing 2
random columns from CSE1, CSE2, CSE3, and CSE4 first, AND then another
random column from WSE1, WSE2, WSE3, and WSE4) for 1000 times. any ideas?

Many thanks in advance!!

-- 
View this message in context: 
http://r.789695.n4.nabble.com/Sampling-problem-tp3043804p3043804.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.