# How to print the proportion of values in a list where P < 0.05?

A few things look wrong to me.

1. Shouldn't the loop over k run from 1 to i?
``````for (k in 1:i){
``````

Looping over 1:29 every time seems to defeat the purpose of having the variable i

1. The result of the rbind() inside of the k for loop is not assigned to any variable. Should it be assigned to df?
``````df <- rbind(df, data.frame(x1 = rep(distances, 2), #added assignment
y = c(rbinom(e,n_trials,pr), rbinom(e,n_trials,pr1)),
groupcategory = groupcategory, id = c(rep(k*2-1, 6), rep(k*2, 6))))
``````
1. The subsetting of pvalue to get the desired value is not assigned to any variable. Maybe you mean
``````pvalue <- pvalue[[1]]["groupcategory","Pr(>F)"]
``````

When you say (K in 1:i)[

is it because you only want to store the data for that particular given sample size on each round and not every sample size?

Yes, that is my understanding but it is your code. Be sure that anything I suggest is correct for your application.

df <- rbind(df, data.frame(x1 = rep(distances, 2),
y = c(rbinom(e,n_trials,pr), rbinom(e,n_trials,pr1)),
groupcategory = groupcategory, id = c(rep(k, 12))))

When I enter this formula, I get:

x1 y groupcategory id
1 60.000 59 1 1
2 90.000 50 1 1
3 135.000 43 1 1
4 202.500 16 1 1
5 303.750 0 1 1
6 455.625 0 1 1
7 60.000 60 2 2
8 90.000 60 2 2
9 135.000 60 2 2
10 202.500 60 2 2
11 303.750 60 2 2
12 455.625 59 2 2

But surely the ID number should be the same for the 12 values...

Yes, you should get a single id value and that is what I get if I comment out the lines for looping and run a single iteration of that code.

``````coef1 <- 5
coef2 <- -0.03
coef3 <- 5
coef4 <- -0.03
distances <- c(60,90,135,202.5,303.75,455.625)
x1 <- distances
z <- coef1 + coef2*x1
z1 <- coef3 + coef4*x1
n_trials <- 60
oneto29 <- 29
oneto1000 <- 10
e <- 6
groupcategory = c(1,1,1,1,1,1,2,2,2,2,2,2)
d = NULL
FracP <- vector(mode="numeric",length=29)
pr = 1/(1+exp(-z))
pr1 = 1/(1+exp(-z1))

#for (i in 1:oneto29) {
#  for (j in 1:oneto1000){
df <- c()
k <- 1
#    for (k in 1:i){ #changed to 1:i
#df <- rbind(df, data.frame(x1 = rep(distances, 2), #added assignment
#                     y = c(rbinom(e,n_trials,pr), rbinom(e,n_trials,pr1)),
#                     groupcategory = groupcategory, id = c(rep(k*2-1, 6), rep(k*2, 6))))
df <- rbind(df, data.frame(x1 = rep(distances, 2),
y = c(rbinom(e,n_trials,pr), rbinom(e,n_trials,pr1)),
groupcategory = groupcategory, id = c(rep(k, 12))))
#}
df
#>         x1  y groupcategory id
#> 1   60.000 57             1  1
#> 2   90.000 53             1  1
#> 3  135.000 45             1  1
#> 4  202.500 15             1  1
#> 5  303.750  0             1  1
#> 6  455.625  0             1  1
#> 7   60.000 56             2  1
#> 8   90.000 56             2  1
#> 9  135.000 45             2  1
#> 10 202.500 15             2  1
#> 11 303.750  0             2  1
#> 12 455.625  0             2  1
``````

Created on 2020-02-03 by the reprex package (v0.3.0)

1 Like

Hey thanks!

I am now plotting a heat map, showing how the proportion of P-values (power) changes depending on the coefficient 3 and coefficient 4.

library(tidyverse)

n_people <- c(2:20)
coef1 <- 5
coef2 <- -0.05
coef3 <- 5
coef4 <- -0.02
distances <- c(60,90,135,202.5,303.75,455.625)
n_trials <- 60
oneto29 <- 29
oneto1000 <- 5
e <- 6
groupcategory = c(1,1,1,1,1,1,2,2,2,2,2,2)

x1 <- distances
z <- coef1 + coef2*x1
Datarray <- array(dim=c(length(coef3s), length(coef4s),length(n_people)))

coef3s <- c(1:11)
coef4s <- seq(from = -0.01, to = -0.1, length.out = 10)

coef3_counter =1
for (coef3 in coef3s) {
coef4_counter =1
for (coef4 in coef4s) {
z1 <- coef3 + coef4*x1
d = NULL
pr = 1/(1+exp(-z))
pr1 = 1/(1+exp(-z1))

``````counter=1
for (i in n_people) {
for (j in 1:oneto1000){
df <- c()
for (k in 1:i){
df <- rbind(df, data.frame(x1 = c(rep(distances, 2)),
y = c(rbinom(e,n_trials,pr), rbinom(e,n_trials,pr1)),
groupcategory = groupcategory, id = c(rep(k,12))))
# y = c(rbinom(e,n_trials,pr), rbinom(e,n_trials,pr1)),
#groupcategory = groupcategory, id = c(rep(k*2-1, 6), rep(k*2, 6))))
}
df_aov <- aov(y~x1*groupcategory+Error(id/(x1*groupcategory)),data=df)
df_aov_sum <- summary(df_aov)
pvalue <- df_aov_sum[[5]]
pvalue <- pvalue[[1]]["groupcategory","Pr(>F)"]
d = rbind(d,data.frame(pvalue))
}
count <- plyr::ldply(d,function(c) sum(c<=0.05))
Datarray[coef3_counter,coef4_counter,counter] <- count\$V1/oneto1000
counter = counter +1
d = NULL

}
coef4_counter = coef4_counter + 1
``````

}
coef3_counter = coef3_counter + 1
}

hv <- heatmap(object2, xlab = "Coef3", ylab = "Coef4",main = "Sample Size N=16",cex.axis=1.2)

I am unable to capp the legend values for my correlation matrix below: (?)

ggcorrplot(object2, aes(x=Coef3,y=Coef4), limits=FALSE)

It produces the correlation matrix, but the legend has values less than 0, and I only want it to have values 0 and above? Also, the y and x axis are also not labelling?

@FJCC

This question seems to be completely different from the preceding question. You should start a new thread, especially since I know nothing about the ggcorrplot() function and may have little to contribute. In that post, please make it clear how to produce the object you are trying to graph. In the above post, I do not see how to make object2. Since your question is about plotting only positive correlations and changing the plot labeling, the object you plot can be a small, simple correlation matrix produced with a few lines of code. You will attract more and better answers if your post is easy to understand because it has a minimal example.

1 Like

I am sorry for causing you trouble! I will create a new thread immediately! @FJCC

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.