Home > Enterprise >  Sample values from a data.frame, run a function and use the results to create a third data.frame
Sample values from a data.frame, run a function and use the results to create a third data.frame

Time:07-16

I'm working with two data.frames, one contains parameter values ("params") and another contains covariate values ("data").

> head(params)
# A tibble: 6 × 4
  `a[1]` `a[2]` `b[1]`    `b[2]`
   <dbl>  <dbl>  <dbl>     <dbl>
1  0.911   1.06 0.240   0.00333 
2  0.882   1.04 0.239   0.0361  
3  0.896   1.04 0.0207  0.0140  
4  0.897   1.03 0.163   0.0150 


> head(data)
  cid   rugged_std
1   1 -0.200000000
2   1 -0.151724138
3   1 -0.103448276
4   1 -0.055172414
5   1 -0.006896552

I would like to create a third data.frame with mu values according to this formula:

mu = a[cid]   b[cid]*rugged_std

The desired output is a data.frame with 30 columns (1 column per covariate value), and 100 rows (100 combinations of a[cid] and b[cid] parameters extracted from params.

Basically, for each rugged_std value, I would like 100 mu values resulting from combinations of a and b parameters. Repetition is fine.

Data:

data<-structure(list(cid = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), rugged_std = c(-0.2, 
-0.151724137931035, -0.103448275862069, -0.0551724137931034, 
-0.00689655172413794, 0.0413793103448276, 0.0896551724137931, 
0.137931034482759, 0.186206896551724, 0.23448275862069, 0.282758620689655, 
0.331034482758621, 0.379310344827586, 0.427586206896552, 0.475862068965517, 
0.524137931034483, 0.572413793103448, 0.620689655172414, 0.668965517241379, 
0.717241379310345, 0.76551724137931, 0.813793103448276, 0.862068965517241, 
0.910344827586207, 0.958620689655173, 1.00689655172414, 1.0551724137931, 
1.10344827586207, 1.15172413793103, 1.2)), class = "data.frame", row.names = c(NA, 
-30L))


params<-structure(list(`a[1]` = c(0.910836, 0.882068, 0.896156, 0.896902, 
0.871198, 0.890379, 0.873814, 0.867861, 0.888607, 0.887889, 0.90401, 
0.904002, 0.863041, 0.902914, 0.866706, 0.87441, 0.908609, 0.899593, 
0.868457, 0.870945, 0.892223, 0.905765, 0.892793, 0.88536, 0.869485, 
0.892779, 0.923214, 0.879923, 0.896627, 0.856263), `a[2]` = c(1.05786, 
1.04465, 1.04209, 1.03482, 1.04724, 1.04936, 1.06329, 1.05086, 
1.0525, 1.03906, 1.05075, 1.04356, 1.03075, 1.04575, 1.04979, 
1.04543, 1.02223, 1.02891, 1.04519, 1.02875, 1.06981, 1.03853, 
1.04947, 1.04073, 1.05104, 1.05636, 1.0441, 1.04498, 1.0579, 
1.0388), `b[1]` = c(0.239597, 0.239137, 0.0206942, 0.162874, 
0.102635, 0.167029, 0.156862, -0.0605341, 0.076628, 0.125264, 
0.251273, 0.221596, 0.126793, 0.0678483, 0.0885772, 0.0389816, 
0.142527, 0.17842, 0.192263, 0.189756, 0.159639, 0.100018, 0.0579971, 
0.0568845, 0.0689558, 0.180105, 0.126894, 0.00137584, 0.233685, 
0.179266), `b[2]` = c(0.00332765, 0.0360513, 0.0140297, 0.0149716, 
0.00192411, -0.000581532, 0.00138268, 0.0241489, 0.0251936, 0.0323527, 
0.008488, 0.00979467, 0.00442379, 0.00860992, 0.0551994, 0.00498295, 
0.0458311, 0.0333743, 0.00265565, 0.0124222, 0.0178482, 0.0284355, 
0.0279484, 0.00664905, 0.0093533, 0.0236543, 0.00727722, 0.0296122, 
0.00174974, 0.00184884)), row.names = c(NA, -30L), class = c("tbl_df", 
"tbl", "data.frame"))

CodePudding user response:

Here is a very simple approach, if I understand what you are trying to do!

First I combine the a parameters from params$a[1] and params$a[2]. Similarly for b. Then, I create a simple function, f, that samples from these params with replacement n times (default n=100) and feeds to your formula. Then just use sapply() over the values of data$rugged_std to return your desired matrix of 100 rows x 30 columns

a = c(params$`a[1]`, params$`a[2]`)
b = c(params$`b[1]`, params$`b[2]`)
f <- function(r,n=100) sample(a,n, replace=T)   sample(b,n, replace=T)*r
result = sapply(data$rugged_std,f)

Output

> dim(result)
[1] 100  30
> head(result)
          [,1]      [,2]      [,3]      [,4]      [,5]      [,6]      [,7]      [,8]      [,9]     [,10]     [,11]     [,12]     [,13]     [,14]
[1,] 0.8401244 0.8719293 0.8513466 1.0572146 1.0450235 0.8939495 1.0658014 1.0446521 1.0575639 0.9237991 0.8597755 0.8695586 0.9475996 1.1120520
[2,] 0.8863085 1.0390077 0.8792717 1.0434050 0.8733282 1.0511370 0.8824287 1.0908444 0.9067674 0.9106279 1.1208396 1.0835960 0.9074122 1.0734043
[3,] 1.0221760 0.8835922 0.8637147 0.9079236 0.9057148 1.0593844 0.9229215 0.8966480 1.0819620 1.0511462 1.0391890 0.8944912 1.0531162 1.0904894
[4,] 0.9030397 0.8664521 1.0433790 1.0599661 1.0493640 0.8929615 0.8588124 0.8915498 0.8887127 1.0225526 1.0557967 1.0760657 0.9748904 0.8620055
[5,] 0.8623755 0.8702738 0.8910174 1.0490898 1.0562925 0.8886866 0.8999746 0.8572668 0.9007571 1.0458127 1.0853809 1.1077924 1.0830682 1.1493905
[6,] 0.8750534 1.0236610 1.0493277 0.8819215 1.0578695 0.8747111 0.8857867 0.8882553 1.0531196 0.8858289 0.8813918 0.9099184 1.1068457 1.0359555
         [,15]     [,16]     [,17]     [,18]     [,19]    [,20]     [,21]     [,22]     [,23]     [,24]     [,25]     [,26]     [,27]     [,28]
[1,] 0.8749841 0.9574869 1.0506456 0.9515935 1.0129671 1.020265 1.0900374 0.8733408 1.0644761 1.0769193 0.9127581 1.0860411 1.2627320 1.0681913
[2,] 1.0537637 1.1277729 1.0436586 1.1534632 1.0419668 1.053880 0.8871580 1.2298024 1.0671486 0.8948183 1.0949603 1.0535409 1.2011406 1.0768071
[3,] 0.9818611 1.0852267 0.9692629 0.9917123 0.9590383 1.072609 0.8841009 0.9714240 1.1015347 1.1662854 1.1688832 1.0543906 0.8968453 1.0859663
[4,] 1.0016393 1.0505552 0.8785756 1.0465776 0.8967766 1.051375 1.0706078 0.9390712 1.0631758 0.9016080 0.9837683 1.0244439 1.0759456 0.8971044
[5,] 0.8704006 1.0681218 1.0637091 0.9695512 1.1793673 1.074430 0.9151134 0.9278587 1.0915048 0.9067279 1.0177024 1.0036218 0.9848658 0.9055357
[6,] 1.0482209 1.0799721 0.8950753 0.9649941 1.1455930 1.010038 1.0630818 1.0202954 0.8908964 1.2078537 1.2297481 0.9009852 0.8896246 1.0884442
         [,29]     [,30]
[1,] 0.9376890 1.0511292
[2,] 1.1307665 1.0860632
[3,] 1.1050836 1.0712689
[4,] 1.0022936 1.0480695
[5,] 0.8342914 1.3450876
[6,] 1.0509290 0.9566183
  • Related