Essentially, I want to build a spider plot for sensitivity analysis. I want to split my data into 10 tranches, and find the mean result value (in column 4) for each tranche. The tranches should be selected based on the 10th, 20th, 30th, 40th, etc. percentiles for the data in each of the variable columns. I got this to work, but I’m thinking that there must be a much easier way to do it.
My code:
##Make some data and put it into a matrix.
c <- 1000
v1 <- rnorm (c, 100, 15)
v2 <- rnorm (c, 80, 10)
v3 <- rnorm (c, 50, 5)
r1 <- ((v1*v2^2)/v3)
data <- cbind (v1,v2)
data <- cbind (data, v3)
data <- cbind (data, r1)
##Sort matrix by first column.
data <- as.matrix(data[order(data[,1]),])
##Find mean of column 4 values corresponding to the smallest 10% (and 20%, and 30%, etc.) of column 1 values.
a1 <- mean (data[1:(c/10),4])
a2 <- mean (data[(c/10):(2*c/10),4])
a3 <- mean (data[(2*c/10):(3*c/10),4])
a4 <- mean (data[(3*c/10):(4*c/10),4])
a5 <- mean (data[(4*c/10):(5*c/10),4])
a6 <- mean (data[(5*c/10):(6*c/10),4])
a7 <- mean (data[(6*c/10):(7*c/10),4])
a8 <- mean (data[(7*c/10):(8*c/10),4])
a9 <- mean (data[(8*c/10):(9*c/10),4])
a10 <- mean (data[(9*c/10):c,4])
##Combine into a vector.
a <- as.vector(c(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10))
##Repeat for data sorted by columns 2 and 3 respectively.
data <- as.matrix(data[order(data[,2]),])
a1 <- mean (data[1:(c/10),4])
a2 <- mean (data[(c/10):(2*c/10),4])
a3 <- mean (data[(2*c/10):(3*c/10),4])
a4 <- mean (data[(3*c/10):(4*c/10),4])
a5 <- mean (data[(4*c/10):(5*c/10),4])
a6 <- mean (data[(5*c/10):(6*c/10),4])
a7 <- mean (data[(6*c/10):(7*c/10),4])
a8 <- mean (data[(7*c/10):(8*c/10),4])
a9 <- mean (data[(8*c/10):(9*c/10),4])
a10 <- mean (data[(9*c/10):c,4])
b <- as.vector(c(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10))
data <- as.matrix(data[order(data[,3]),])
a1 <- mean (data[1:(c/10),4])
a2 <- mean (data[(c/10):(2*c/10),4])
a3 <- mean (data[(2*c/10):(3*c/10),4])
a4 <- mean (data[(3*c/10):(4*c/10),4])
a5 <- mean (data[(4*c/10):(5*c/10),4])
a6 <- mean (data[(5*c/10):(6*c/10),4])
a7 <- mean (data[(6*c/10):(7*c/10),4])
a8 <- mean (data[(7*c/10):(8*c/10),4])
a9 <- mean (data[(8*c/10):(9*c/10),4])
a10 <- mean (data[(9*c/10):c,4])
d <- as.vector(c(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10))
##Make a pretty chart
plot (a, type = "o", col = "red")
lines (b, type = "o", col = "blue")
lines (d, type = "o", col = "green")
Here is some code that does the same thing, but a more compactly and idiomatically for R.
Then you can make the plot like you did before.
EDIT:
Ananda Mahto’s answer pointed out the function version of the aggregate function which I had forgotten about. You could write the
aggregatelines more clearly as