R: how to count the number of times two elements have the same ID (perhaps using the outer function) - r

I have the following three dimensional array:
dput(a)
structure(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, 3, 3, 2, 1, 1, 1, 2, 2,
2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 6, 2, 7, 6, 2, 7, 6, 2, 7, 4, 2, 4, 4, 2, 6, 4, 2, 4, 6, 2,
7, 4, 2, 6, 4, 2, 6, 4, 2, 6, 4, 2, 4, 4, 2, 6, 4, 2, 4, 4, 2,
6, 4, 2, 6, 4, 2, 6, 6, 2, 7, 4, 2, 6, 4, 2, 6, 4, 2, 4, 2, 3,
1, 2, 3, 1, 2, 3, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 2, 3, 7, 2, 3,
7, 2, 3, 7, 2, 3, 7, 2, 3, 7, 2, 3, 7, 2, 3, 7, 2, 3, 7, 2, 3,
7, 2, 3, 7, 1, 2, 5, 2, 3, 7, 1, 2, 4, 2, 3, 7, 2, 3, 7, 2, 3,
7, 2, 3, 7, 2, 3, 7, 2, 3, 7, 2, 3, 7, 2, 6, 3, 2, 6, 3, 2, 6,
3, 2, 6, 3, 2, 6, 3, 2, 6, 3, 2, 6, 3, 2, 6, 3, 2, 6, 3, 2, 6,
3, 1, 1, 1, 2, 6, 3, 1, 5, 5, 2, 6, 3, 2, 6, 3, 2, 6, 3, 2, 6,
3, 2, 6, 3, 2, 6, 3, 2, 6, 3, 3, 3, 2, 3, 3, 2, 3, 3, 2, 3, 13,
2, 3, 13, 2, 3, 5, 2, 3, 5, 2, 15, 17, 2, 15, 17, 2, 15, 17,
2, 3, 5, 2, 15, 17, 2, 3, 13, 2, 15, 17, 2, 15, 17, 2, 3, 13,
2, 3, 5, 2, 15, 17, 2, 15, 17, 2, 3, 5, 2), .Dim = c(3L, 20L,
6L), .Dimnames = list(c("cl.tmp", "cl.tmp", "cl.tmp"), NULL,
NULL))
The dimension of this array (a) is 3x20x6 (after edits).
I wanted to count the proportion of times that a[,i,] matches a[,j,] element-by-element in the matrix. Basically, I wanted to get mean(a[,i,] == a[,j,]) for all i, j, and I would like to do this fast but in R.
It occurred to me that the outer function might be a possibility but I am not sure how to specify the function. Any suggestions, or any other alternative ways?
The output would be a 20x20 symmetric matrix of nonnegative elements with 1 on the diagonals.
The solution given below works (thanks!) but I have one further question (sorry).
I would like to display the coordinates above in a heatmap. I try the following:
n<-dim(a)[2]
xx <- matrix(apply(a[,rep(1:n,n),]==a[,rep(1:n,each=n),],2,sum),nrow=n)/prod(dim(a)[-2])
image(1:20, 1:20, xx, xlab = "", ylab = "")
This gives me the following heatmap.
However, I would like to display (reorder the coordinate) such that I get all the coordinates that have high-values amongst each other together. However, I would not like to bias the results by deciding on the number of groups myself. I tried
hc <- hclust(as.dist(1-xx), method = "single")
but I can not decide how to cut the resulting tree to decide on bunching the coordinates together. Any suggestions? Bascically, in the figure, I would like the coordinate pairs in the top left (and bottom right off-diagonal blocks) to be as low-valued (in this case as red) as possible.
Looking around on SO, I found that there exists a function heatmap which might do this,
heatmap(xx,Colv=T,Rowv=T, scale='none',symm = T)
and I get the following:
which is all right, but I can not figure out how to get rid of the dendrograms on the sides or the axes labels. It does work if I extract out and do the following:
yy <- heatmap(xx,Colv=T,Rowv=T, scale='none',symm = T,keep.dendro=F)
image(1:20, 1:20, xx[yy$rowInd,yy$colInd], xlab = "", ylab = "")
so I guess that is what I will stick with. Here is the result:

Try this:
n<-dim(a)[2]
matrix(apply(a[,rep(1:n,n),]==a[,rep(1:n,each=n),],2,sum),nrow=n)/prod(dim(a)[-2])
It has to be stressed that the memory usage of this method goes with n^2 so you might have trouble to use it with larger arrays.

Related

Clean up list output within RStudio when using lapply() or sapply()

I have a function that I am using with lapply().
lapply(x, function(x))
The output looks like this:
[[1]]
[1] "att_admire"
[[2]]
[1] "att_hypocrisy"
[[3]]
[1] "att_annoyed"
[[4]]
[1] ""
[[5]]
[1] "att_respectvalues"
When I use sapply(), it looks like this:
att_admire att_hypocrisy att_annoyed att_judge
"att_admire" "att_hypocrisy" "att_annoyed" ""
att_respectvalues
"att_respectvalues"
I would like the output to look neater, like this. Only the variable names should be included, not the "" or indices.
att_admire, att_hypocrisy, att_annoyed, att_respectvalues
What can I do to modify the code I have? I looked at this question, but it focuses on how to get cleaner results in an output file, whereas I would prefer for the output to look cleaner in RStudio itself.
Below, I have included the function I am using and a sample of the data.
Libraries:
install.packages("lavaan")
library(lavaan)
Main command:
iv <- "consistent"
dv <- "behavior_solarize"
mediators <- c("att_admire", "att_hypocrisy", "att_annoyed", "att_judge", "att_respectvalues")
lapply(mediators, med_overall)
Function (using "lavaan" package):
med_overall <- function(mediator) {
mediation.model <-
paste("
# mediator
",mediator," ~ a * ",iv,"
",dv," ~ b * ",mediator,"
# direct effect
",dv," ~ c * ",iv,"
# indirect effect (a*b)
ab := a*b
# total effect
total := c + (a*b)")
fit <- sem(mediation.model, data = df, meanstructure = TRUE,
se = "boot", bootstrap = 500)
val <- parameterEstimates(fit)
ifelse(val[10,8] < 0.05, mediator, "")}
Sample data:
structure(list(behavior_solarize = c(5, 5, 5, 3, 3, 3, 5, 6,
7, 5, 6, 7, 4, 3, 6, 4, 5, 6, 2, 4, 4, 5, 5, 5, 6, 2, 6, 2, 2,
1, 5, 5, 5, 5, 1, 5, 1, 5, 1, 6, 4, 2, 6, 6, 7, 6, 5, 5, 6, 5,
1, 4, 1, 6, 7, 6, 6, 3, 4, 2, 6, 7, 6, 1, 1, 2, 2, 5, 5, 4, 6,
3, 2, 5, 6, 1, 6, 2, 2, 6, 5, 3, 2, 3, 5, 1, 2, 1, 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names = c(NA,
-693L), class = c("tbl_df", "tbl", "data.frame"))
Store the results in an object. Then exclude empty strings (where do they actually come from?) convert toString and cat it.
res <- lapply(mediators, med_overall)
cat(toString(res[res != '']))
# att_admire, att_hypocrisy, att_annoyed, att_respectvalues

Output list of variables with significant p-values from several regressions in R

I have a dataframe that looks like the following.
consistent admire trust judge
3 3 2 4
5 1 3 6
2 4 5 1
I can run the regressions I need simultaneously using the following code. In the actual dataset, there are many more than 3 variables.
variables <- c("admire", "trust", "judge")
form <- paste("consistent ~ ",variables,"")
model <- form %>%
set_names(variables) %>%
map(~lm(as.formula(.x), data = df))
map(model, summary)
This yields the output for the 3 following regressions.
summary(lm(consistent ~ admire, df))
summary(lm(consistent ~ trust, df))
summary(lm(consistent ~ judge, df))
I would like a list of the variables with significant p-values at p < 0.05. For example, if "admire" was significant and "judge" was significant, the output I am looking for would be something like:
admire, judge
Is there a way to do this that allows me to also run several regressions simultaneously? This question offers a similar answer, but I don't know how to apply it when I have several regressions.
Data:
structure(list(consistent = c(1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0,
1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0,
1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1,
0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1,
1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1,
1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0,
1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1,
1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1,
0, 0, 1, 0, 0, 1, 0, 0, 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2, 6, 3, 1, 1, 1, 1,
4, 2, 2, 4, 4, 5, 4, 2, 1, 6, 7, 5, 2, 2, 4, 5, 6, 1, 5, 2, 4,
5, 5, 2, 2, 3, 4, 5, 2, 2, 4, 1, 3, 4, 4, 4, 2, 3, 1, 4, 4, 3,
2, 3, 1, 4, 2, 4, 4, 1, 5, 4, 4, 4, 4, 6, 1, 3, 5, 7, 2, 6, 1,
5, 7, 5, 4, 2, 3, 6, 3, 1, 1, 2, 2, 5, 5, 2, 5, 4, 4, 5, 4, 4,
3, 7, 4, 4, 4, 2, 5, 3, 6, 5, 4, 4, 4, 6, 4, 5, 5, 1, 5, 2, 6,
4, 4, 1, 1, 4, 6, 1, 7, 1, 5, 2, 5, 4, 2, 3, 2, 6, 3, 2, 2, 1,
1, 5, 4, 1, 1, 4, 1, 5, 1, 4, 3, 2, 3, 4, 1, 6, 1, 2, 1, 3, 5,
5, 2, 1, 3, 4, 2, 4, 5, 4, 6, 3, 4, 6, 7, 6, 2, 4, 6, 2, 4, 5,
1, 4, 1, 3, 2, 4, 1, 6, 4, 3, 1, 3, 4, 5, 1, 6, 1, 5, 1, 3, 3,
1, 3, 4, 2, 4, 1, 1, 2, 2, 2, 3, 1, 6, 5, 4, 1, 7, 5, 6, 5, 2,
3, 5, 4, 3, 4, 5, 7, 1, 5, 2, 5, 1, 3, 4, 3, 5, 1, 4, 2, 3, 4,
1, 7, 5, 5, 2, 1, 2, 5, 6, 5, 5, 3, 1, 3, 1, 4, 1, 5, 2, 3, 5,
6, 4, 4, 3, 2, 4, 1, 3, 4, 3, 4, 4, 1, 5)), row.names = c(NA,
-450L), class = c("tbl_df", "tbl", "data.frame"))
To fit many many simple linear regression models, I recommend Fast pairwise simple linear regression between variables in a data frame. Hmm... looks like I need to collect those functions in an R package...
## suppose your data frame is `df`
## response variable (LHS) in column 1
## independent variable (RHS) in other columns
out <- general_paired_simpleLM(df[1], df[-1])
# LHS RHS alpha beta beta.se beta.tv beta.pv
#1 consistent admire -0.1458455 0.18754326 0.008324192 22.529906 1.040756e-75
#2 consistent trust -0.2211250 0.19565589 0.007721387 25.339475 1.531499e-88
#3 consistent judge 0.3484851 0.04824981 0.014182420 3.402086 7.287372e-04
# sig R2 F.fv F.pv
#1 0.3430602 0.53118295 507.59665 1.040756e-75
#2 0.3212008 0.58902439 642.08902 1.531499e-88
#3 0.4946862 0.02518459 11.57419 7.287372e-04
To get what you want:
with(out, RHS[beta.pv < 0.05])
#[1] "admire" "trust" "judge"

Plotly does not plot a non-quadratic surface

I want to produce a 3D scatterplot and add a surface fitted with a linear regression, using plotly. My data:
structure(list(political_trust = c(1, 6, 7, 5, 0, 2, 1, 3, 5,
0, 2, 5, 5, 6, 6, 3, 3, 2, 5, 8, 3, 7, 3, 4, 5, 4, 5, 0, 0, 4,
6, 1, 0, 4, 0, 5, 5, 6, 7, 3, 5, 4, 5, 2, 4, 4, 7, 6, 7, 5, 4,
6, 7, 5, 7, 3, 3, 3, 2, 5, 2, 7, 3, 2, 7, 2, 3, 0, 7, 5, 7, 3,
0, 7, 2, 6, 3, 8, 7, 2, 2, 5, 0, 1, 6, 3, 6, 5, 1, 3, 4, 4, 5,
3, 3, 0, 2, 4, 9, 6, 3, 3, 2, 3, 4, 5, 8, 0, 4, 1, 5, 0, 4, 0,
5, 6, 3, 2, 7, 5, 4, 3, 8, 3, 4, 0, 3, 6, 7, 7, 2, 3, 5, 5, 5,
0, 3, 2, 1, 7, 5, 0, 4, 0, 2, 7, 3, 0, 8, 3, 2, 4, 5, 5, 3, 2,
3, 8, 6, 5, 6, 7, 0, NA, 7, 7, 2, 0, 3, 4, 7, 2, 1, 2, 0, 0,
4, 3, 3, 6, 6, 1, 4, 0, 4, 0, 0, 7, 6, 4, 4, 6, 5, 4, 3, 3, 0,
NA, 2, 5), political_interest = c(2, 0, 3, 3, 2, 1, 2, 2, 2,
2, 2, 2, 3, 3, 3, 3, 2, 2, 3, 2, 1, 2, 2, 2, 2, 0, 2, 1, 3, 1,
1, 1, 1, 1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2, 4,
4, 3, 0, 4, 0, 4, 4, 3, 0, 4, 4, 3, 5, 0, 3, 0, 4, 3, 0, 3, 3,
3, 4, 5, 1)), row.names = c(NA, -200L), class = "data.frame")
I start by defining a list of relevant variables - this is not necessary but basically a consequence of using the code in a Shiny up:
input <- list()
input$x <- "education_level"
input$y <- "political_trust"
input$z <- "political_interest"
Next, creating the surface data:
# Regressing "political_interest" on "education_level" and "political_trust":
lm <- lm(as.formula(paste0(input$z, " ~ ", input$x, " + ", input$y)), data)
# Defining range of values that outcome will be predicted for
axis_x <- seq(min(data[, input$x], na.rm = T),
max(data[, input$x], na.rm = T), by = 0.2)
axis_y <- seq(min(data[, input$y], na.rm = T),
max(data[, input$y], na.rm = T), by = 0.2)
# Predicting outcome, and getting data into surface format
lm_surface <- expand.grid(x = axis_x, y = axis_y, KEEP.OUT.ATTRS = F)
colnames(lm_surface) <- c(input$x, input$y)
lm_surface <- acast(lm_surface, as.formula(paste0(input$x, " ~ ", input$y)),
value.var = input$z)
Last, plotting this with plotly:
data %>%
filter(!is.na(get(input$z))) %>%
filter(!is.na(get(input$x))) %>%
filter(!is.na(get(input$y))) %>%
plot_ly(., x = ~jitter(get(input$x), factor = 2.5),
y = ~jitter(get(input$y), factor = 2.5),
z = ~jitter(get(input$z), factor = 2.5),
type = "scatter3d", mode = "markers",
marker = list(size = 2, color = "#cccccc")) %>%
add_surface(., z = lm_surface,
x = axis_x,
y = axis_y,
type = "surface")
This gives me the following. As you can see, the surface does not cover the full range of the y-dimension. Note also that the surface plotted is "quadratic" - i.e. same length in x and y - although it should have non-quadratic dimensions.
I can bring plotly to draw larger surface area, e.g. by changing the range of values like below, but it always stays quadratic.
axis_x <- seq(0, 10, by = 0.2)
axis_y <- seq(0, 10, by = 0.2)
Ok, question solved.
It's important which dimension of the surface matrix (lm_surface) is which. Swapping x and y when applying acast fixes the issue:
lm_surface <- acast(lm_surface, as.formula(paste0(input$y, " ~ ", input$x)),
value.var = input$z)

Counting specific sets of repeats

I am having to write code in r where I have to count the number of times a specific set of numbers within a vector are repeated one after another.
For example, in the following set of numbers, I would want to count the number of times a number was repeated after itself, such as 2,2 and 4,4, or even repeating after itself 3 times in a row such as 1,1,1 or 3,3,3, not counting the number of times an individual number has occurred throughout the set.
5, 3, 2, 2, 4, 1, 4, 4, 6, 1, 3, 2, 1, 4, 3, 1, 6, 4, 5, 5, 3, 4, 3, 4, 4, 5, 6, 6, 2, 4, 6, 1, 1, 1, 2, 2, 4, 3, 3, 3, 1, 3, 5, 1, 5, 2, 2, 6, 5, 6, 3
You can use rle to find repeated consecutive values. For example,
i1 <- rle(x)
setNames(i1$lengths[i1$lengths > 1], paste0('value:', i1$values[i1$lengths > 1]))
#value:2 value:4 value:5 value:4 value:6 value:1 value:2 value:3 value:2
# 2 2 2 2 2 3 2 3 2
DATA
dput(x)
c(5, 3, 2, 2, 4, 1, 4, 4, 6, 1, 3, 2, 1, 4, 3, 1, 6, 4, 5, 5,
3, 4, 3, 4, 4, 5, 6, 6, 2, 4, 6, 1, 1, 1, 2, 2, 4, 3, 3, 3, 1,
3, 5, 1, 5, 2, 2, 6, 5, 6, 3)

Generate a sequence number (1,1,1,2,2,2,3,3,3) within groups of different length

I have a data frame with a column "Tag", here with four different levels. I need help to create the "Seq" column, a sequence generated from the "Tag" Column:
df <- data.frame(Tag = 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, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4),
Seq = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3 )
Each "Tag" should be divided into 3 sub-groups defined by "Seq". We need to generate runs of 1, 2, and 3, with a total length of that of each "Tag". Thus, the length of each run of 1, 2, and 3 respectively depends on length of each "Tag".
Note that the length each "Tag" differs. For example, Tag 1 is of length 31, and has a "Seq" 10 times 1, 10 times 2, and 11 times 3.
To begin with, Tag 1 is 31 while tag 2 is 32. Looking at the code below, the first number (1) will always be of lesser length than the next two (2,3). I used a ceiling process to come up with this. There is no clear criteria on what the code should do if the number is eg 31/3.. should it give a length of 10, 10, 11? or even 9, 11,11 will be fine? The code gives a 9, 11, 11 length:
ec=table(Tag)
unlist(mapply(function(x,y)rep(c(1,2,3),c(x,y,y)),ec-2*ceiling(ec/3),ceiling(ec/3)))
To check the outputted results, save the results in a variable.. d=mapply(...
then do sapply(d,table).
Hope this will be of help.
ave(Tag, Tag, FUN = function(x){sort(rep(x = 1:3, length.out = length(x)))})
Explanation: For each level of "Tag" (ave(Tag, Tag, ...): repeat each level of "Seq" (x = 1:3) to the length of the subset of "Tag" (length.out = length(x)). sort the numbers.

Resources