vendredi 26 août 2016

How to assign multiple event IDs per column in a matrix

I am newbie to R and learning Introduction to R. I have a large matrix (multiples of 100000s) and a few hundred columns. I am giving an example matrix herebelow.

set.seed(1)
m4 <- matrix(sample(0:3,5*4, replace=TRUE),5,4) # sample event matrix
m4GROUP <- data.frame( X1=rowSums(m4[,1, drop=FALSE]), X2=rowSums(m4[,2:3]), 
                       X3=rowSums(m4[,4, drop=FALSE]) ) # 1 column
m4GroupColID <- colSums(m4GROUP) # coln sum to generate a matrix per col

Output

m4
     [,1] [,2] [,3] [,4]
[1,]    1    3    0    1
[2,]    1    3    0    2
[3,]    2    2    2    3
[4,]    3    2    1    1
[5,]    0    0    3    3

> m4GROUP # group by Col 1, Cols 2-3, Col4
  X1 X2 X3
1  1  3  1
2  1  3  2
3  2  4  3
4  3  3  1
5  0  3  3

> m4GroupColID
X1 X2 X3 
 7 16 10 

  • I need to generate a matrix of 5 x 4 x total eventsIDs per Row by Group. In this case, I need to get 4 matrix for 4 cols with a dimension of 5 x max(M4Group) per column.
  • For Every Row, the IDs are replaced (i.e. same IDs can repeat, if we have a grouping of two or more columns, between columns forming the group)
  • Between Row, the IDs are not replaced (i.e IDs of previous Row can not be allocated for the next row in the same group having two or more columns); however, IDs of the current row can be allocated between columns in the group.
  • Finally, for groups formed by more than one column, the totalIDs per row can be randomly allocated. This ensures, the group having more than one column are impacted by the ID.

I'm sorry this is a long post and I just do not know how to shorten this without losing the essence.

Total event IDs are 7 for col 1, 16 for cols 2-3 and 10 for col.4:

The final outputs, for example with a random allocation in cols 2-3, per column are as follows: IDs for cols are: 1-7, 8-23, 24-33.

Col 1    Col 2        Col 3        col 4
1 0 0    8 9 10 0     0 0 0 0      24 0 0 
2 0 0    11 12 13 0   0 0 0 0      25 26 0
3 4 0    14 15 17 0   14 16 0 0    27 28 29
5 6 7    18 20 0 0    18 0 0 0     30 0 0 
0 0 0    0 0 0 0      21 22 23 0   31 32 33

If we have just one event per row, the id generation and distribution are fairly straightforward.

m1 <- matrix(sample(0:1,5*4, replace=TRUE),5,4) # sample event matrix
ifelse(m1==0, 0, matrix(sample(1:1,5*4, replace = T), 5,4)) # works for one  ID assignment

I started with a loop, with a knowledge, for loops are not faster in R. But,I am getting errors, irrespective of with or without replacement. I definitely need (Replacement = True on the same row) and (Replacement = False) between Rows.

nc <- max(m4GROUP$X1)
i <- 1
j <- 1
while(i <- 5){
  if(m4[i,j] == 0){
    m[i,j] <- matrix(0, 5, nc)
  } else {
    if(m4[i,j] == 1){
      m[i,j] <- matrix(sample(1, i*nc, replace=T), 5, nc)
    } else {
      m[i,j] <- matrix(sample(2, i*nc, replace=T), 5, nc)
    }
  }
}

I'm aware it is not correct. I get the following errors.

Error in m[i, j] <- matrix(sample(0, i * nc, replace = T), 5, nc) : 
  number of items to replace is not a multiple of replacement length
Error in m[i, j] <- matrix(0, 5, nc) : 
  number of items to replace is not a multiple of replacement length

I also tried the following: I do not get results that satisfy the above bullet points; further, they are starting with the same ID for every row. I get only 3 rows and not 4 rows. Finally, this is not ideal, considering my actual sample size.

size <- c(1:7, 1:16, 1:9, 10)
startID <- which(size==1)
endIDs <- c(which(size==1)[-1] -1, length(size))
mats <- mapply(function(x, y) t(size[seq(x, y)]), startID, endIDs)
library(plyr)
m <- (rbind.fill.matrix(mats))

output:

m
     1 2 3 4 5 6 7  8  9 10 11 12 13 14 15 16
[1,] 1 2 3 4 5 6 7 NA NA NA NA NA NA NA NA NA
[2,] 1 2 3 4 5 6 7  8  9 10 11 12 13 14 15 16
[3,] 1 2 3 4 5 6 7  8  9 10 NA NA NA NA NA NA

I am once again sorry for the long post but thanks for not only reading this, but also also the help. Thanks ton.




Selecting a Random Row in Oracle

I need to randomly select values from one table, e.g. tableA.a_id which is a VARCHAR2, and use the value to insert into another table. For instance, assuming three columns needs to be inserted into 100 rows of tableX (a sequence number, a random number between 100 and 999, and values of tableA.a_id):

insert into tableX
select
    rownum,
    dbms_random.value(100,999), 0),
    (select a_id from 
    (
      SELECT a_id 
      FROM tableA
      ORDER BY dbms_random.value
    )
    where rownum = 1)
from
   (select level from dual connect by level <= 100);

However, rather than picking a random row from tableA.a_id for each row, it selects the same value for all the rows, e.g.:

1 129 A-ID-48
2 849 A-ID-48
3 367 A-ID-48

However, if I execute the subquery repeatedly, I get a new value each time (for obvious reason), e.g.:

select a_id from 
    (
      SELECT a_id 
      FROM tableA
      ORDER BY dbms_random.value
    )
where rownum = 1;

Result would be after each execution:

A-ID-7
A-ID-48
A-ID-74

How do I alter the original query, or come up with a new one for that matter, that would insertion of random rows from tableA's a_id column for each insert row into the destination table? Desire outcome:

1 129 A-ID-7
2 849 A-ID-48
3 367 A-ID-74




Java Math.abs random vs. nextInt()

Creating a random number generator in Java - looking at num1 and num2, in which instances would I use either of these methods for creating a random number between 1 and 8? Is one more efficient than the other, any other benefits?

import java.util.*;

public class Chpt3 {
  public static void main(String[] args) {
    Random rand = new Random();
    int num1 = Math.abs(rand.nextInt()) % 8 + 1;
    int num2 = rand.nextInt(8) + 1;
    System.out.println(num1);
    System.out.println(num2);

  }
}




Split dataset into half, randomly selecting half of each level of chosen variable

I know how to split the dataset in half completely randomly no problem, but while I understand "logically" how to go about what I want to do, the twist here seems to be throwing me off.

So I have a dataset with a categorical variable Title which has 120 levels. Each level has 50 observations. I'd like to split the dataset in half in such a way where each of halfset A and halfset B get a random 25 of the 50 observations for each level of Title. (this is for EFA and CFA)

I think it would involve a for loop to loop through the 120 levels and sample(nrow(subset(dataset,title=index), 25), but I'm a little lost beyond that. What little potential solutions I've thought of does the selecting a random 25 for halfset A but with replacement, so when I run it again to make halfset B, it has some overlap.

Thanks as always, everyone.




How to make this javascript, show random post by it's label?

I have searching about random post and it's show Math.random() function, but because my lack of knowledge about javascripting so I need your help to make this javascript work on random, thanks.

<script type='text/javascript'>
//<![CDATA[[
  function CompletedProject(json) {
    for (var i = 0; i < json.feed.entry.length; i++) {
      var judulPost = json.feed.entry[i].title.$t;
      var thumbPost = json.feed.entry[i].media$thumbnail.url;
      //var linkPost = json.feed.entry[i].link[1].href;
      var linkPost;  
    // Get rel=alternate for truly post url
    for (var j=0; j < json.feed.entry[i].link.length; j++)
    {
      if (json.feed.entry[i].link[j].rel == 'alternate')
      {
        linkPost = json.feed.entry[i].link[j].href;
        break;
      }
    }

      var showcompleted = '<div class="ani-item"><a href="'+linkPost+'"><img alt="'+judulPost+'" class="shine" data-original="'+thumbPost+'"/><h4>'+judulPost+'</h4></a></div>';
      document.write(showcompleted);
    }
  }
//]]>
</script>

  <script src='/feeds/posts/default/-/Complete?alt=json-in-script&amp;callback=CompletedProject'/>




C++ Random number generator from a specific array

I want to be able to generate random numbers from a specific array that I will place. For example: I want to generate a random number from the array {3,8,16,19,22}. its just that there is no pattern in the array that I want to generate.

I was only able to search how to generate a random number from 1-100 using srand() from the video tutorial https://www.youtube.com/watch?v=P7kCXepUbZ0&list=PL9156F5253BE624A5&index=16 but I don't know how to specify the array that it will search from..

btw, my code is similar to this..

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <ctime>

using namespace std;

int main(int argc, char*argv[])
{
    srand(time(0)); 

    int i =rand()%100+1;0
    cout << i << endl; 
    return 0;
}




Inverse Laplace trasnform using Mathematica

can any one tell me how they got equation (4) out of (3)? Attached is the equations. They say that they did the inverse laplace trasnform to get it, but i don'y know how! if any one used Mathematica to solve it, that would be more than enough. where equation(3) is the moment generating function and equation (4) is the pdf.

enter image description here

enter image description here