I've used Excel in the past to calculate my results using the poisson function but it took too long so I decided to switch to a more efficient application/language. I've tried SQL (recognised that it might not be an ideal solution for statistical issues), I've tried Matlab (got not used to it at all), meanwhile I've tried R (R Studio) since a couple of days.
To solve my problem I've figured out the following code (it works but one part is missing!):
a and b are examples for input values
a = 1.2
b = 0.7
dpoisson <- function(z,a) {
xlambda <- dpois(z, a)
return(xlambda)
}
dpoissonb <- function(z,b) {
xlambda <- dpois(z, b)
return(xlambda)
}
z = 0:20
dpoisson(z,a)
dpoisson(z,b)
"Calculate Poisson analogous to Excel: e.g. POISSON(x;mean(a);FALSE)*POISSON(x;mean(b);FALSE)"
result <- data.frame(matrix(nrow = 21, ncol = 22))
colnames(result) <- c("Event","A0","A1","A2","A3","A4","A5","A6","A7","A8","A9","A10","A11","A12","A13","A14","A15","A16","A17","A18","A19","A20")
for (i in z) {
for (j in i+1) {
A0 <- dpoisson(i,a)*dpoissonb(0,b)
A1 <- dpoisson(i,a)*dpoissonb(1,b)
A2 <- dpoisson(i,a)*dpoissonb(2,b)
A3 <- dpoisson(i,a)*dpoissonb(3,b)
A4 <- dpoisson(i,a)*dpoissonb(4,b)
A5 <- dpoisson(i,a)*dpoissonb(5,b)
A6 <- dpoisson(i,a)*dpoissonb(6,b)
A7 <- dpoisson(i,a)*dpoissonb(7,b)
A8 <- dpoisson(i,a)*dpoissonb(8,b)
A9 <- dpoisson(i,a)*dpoissonb(9,b)
A10 <- dpoisson(i,a)*dpoissonb(10,b)
A11 <- dpoisson(i,a)*dpoissonb(11,b)
A12 <- dpoisson(i,a)*dpoissonb(12,b)
A13 <- dpoisson(i,a)*dpoissonb(13,b)
A14 <- dpoisson(i,a)*dpoissonb(14,b)
A15 <- dpoisson(i,a)*dpoissonb(15,b)
A16 <- dpoisson(i,a)*dpoissonb(16,b)
A17 <- dpoisson(i,a)*dpoissonb(17,b)
A18 <- dpoisson(i,a)*dpoissonb(18,b)
A19 <- dpoisson(i,a)*dpoissonb(19,b)
A20 <- dpoisson(i,a)*dpoissonb(20,b)
result[j,1] <- i
result[j,2] <- A0
result[j,3] <- A1
result[j,4] <- A2
result[j,5] <- A3
result[j,6] <- A4
result[j,7] <- A5
result[j,8] <- A6
result[j,9] <- A7
result[j,10] <- A8
result[j,11] <- A9
result[j,12] <- A10
result[j,13] <- A11
result[j,14] <- A12
result[j,15] <- A13
result[j,16] <- A14
result[j,17] <- A15
result[j,18] <- A16
result[j,19] <- A17
result[j,20] <- A18
result[j,21] <- A19
result[j,22] <- A20
}
}
probA0_ <- result$A0
probA1_ <- result$A1
probA2_ <- result$A2
probA3_ <- result$A3
probA4_ <- result$A4
probA5_ <- result$A5
probA6_ <- result$A6
probA7_ <- result$A7
probA8_ <- result$A8
probA9_ <- result$A9
probA10_ <- result$A10
probA11_ <- result$A11
probA12_ <- result$A12
probA13_ <- result$A13
probA14_ <- result$A14
probA15_ <- result$A15
probA16_ <- result$A16
probA17_ <- result$A17
probA18_ <- result$A18
probA19_ <- result$A19
probA20_ <- result$A20
"Round values"
probA0_r <- round(probA0_,4)
probA1_r <- round(probA1_,4)
probA2_r <- round(probA2_,4)
probA3_r <- round(probA3_,4)
probA4_r <- round(probA4_,4)
probA5_r <- round(probA5_,4)
probA6_r <- round(probA6_,4)
probA7_r <- round(probA7_,4)
probA8_r <- round(probA8_,4)
probA9_r <- round(probA9_,4)
probA10_r <- round(probA10_,4)
probA11_r <- round(probA11_,4)
probA12_r <- round(probA12_,4)
probA13_r <- round(probA13_,4)
probA14_r <- round(probA14_,4)
probA15_r <- round(probA15_,4)
probA16_r <- round(probA16_,4)
probA17_r <- round(probA17_,4)
probA18_r <- round(probA18_,4)
probA19_r <- round(probA19_,4)
probA20_r <- round(probA20_,4)
"Update A0..A20 with rounded values"
result$A0 <- probA0_r
result$A1 <- probA1_r
result$A2 <- probA2_r
result$A3 <- probA3_r
result$A4 <- probA4_r
result$A5 <- probA5_r
result$A6 <- probA6_r
result$A7 <- probA7_r
result$A8 <- probA8_r
result$A9 <- probA9_r
result$A10 <- probA10_r
result$A11 <- probA11_r
result$A12 <- probA12_r
result$A13 <- probA13_r
result$A14 <- probA14_r
result$A15 <- probA15_r
result$A16 <- probA16_r
result$A17 <- probA17_r
result$A18 <- probA18_r
result$A19 <- probA19_r
result$A20 <- probA20_r
"Sum certain parts of the matrix containing the poisson calculations"
sumpoisson1 = sum(result[2:21,2])+sum(result[3:21,3])+sum(result[4:21,4])+sum(result[5:21,5])+sum(result[6:21,6])+sum(result[7:21,7])+sum(result[8:21,8])+sum(result[9:21,9])+sum(result[10:21,10])+sum(result[11:21,11])+sum(result[12:21,12])+sum(result[13:21,13])+sum(result[14:21,14])+sum(result[15:21,15])+sum(result[16:21,16])+sum(result[17:21,17])+sum(result[18:21,18])+sum(result[19:21,19])+sum(result[20:21,20])+sum(result[21,21])
sumpoisson2 = sum(result[1,2])+sum(result[2,3])+sum(result[3,4])+sum(result[4,5]+sum(result[5,6])+sum(result[6,7])+sum(result[7,8])+sum(result[8,9])+sum(result[9,10])+sum(result[10,11])+sum(result[11,12])+sum(result[12,13])+sum(result[13,14])+sum(result[14,15])+sum(result[15,16])+sum(result[16,17])+sum(result[17,18])+sum(result[18,19])+sum(result[19,20])+sum(result[20,21])+sum(result[21,22]))
sumpoisson3 = sum(result[1,3])+sum(result[1:2,4])+sum(result[1:3,5]+sum(result[1:4,6])+sum(result[1:5,7])+sum(result[1:6,8])+sum(result[1:7,9])+sum(result[1:8,10])+sum(result[1:9,11])+sum(result[1:10,12])+sum(result[1:11,13])+sum(result[1:12,14])+sum(result[1:13,15])+sum(result[1:14,16])+sum(result[1:15,17])+sum(result[1:16,18])+sum(result[1:17,19])+sum(result[1:18,20])+sum(result[1:19,21]))
sumpoisson1
sumpoisson2
sumpoisson3
To summarise: My main goal is to get a matrix where each cell contains the corresponding result of a calculation of the following form: POISSON(x, μ) * POISSON(x, μ)
where x runs from 1 to 20 and μ=a in the first part and μ=b in the second part.
As I've set a und b manually it's really static. I've actually got a tbl_df with two columns containing the double values which are the input values for the poisson distribution calculations. So I gutes I need to embed the already existing code into another for-loop that runs through all records of the two columns with the input values (they should be assigned to a and b).
Since the final goal is to show only the three columns in the end (sumpoisson1,sumpoisson2, sumpoisson3 where each of them is the sum of a certain part of the matrix with the poisson calculations), I want to create these columns next to the two input value columns showing the three results for each specific record of input values.
Is there anybody able to help? Many thanks!!