logo
Tags down

shadow

In R & dabestr, how do I get grouped differences correctly?


By : beachbatsch
Date : October 18 2020, 08:10 AM
around this issue Using dabestr package I'm trying to get the differences between two sets of control & test data. Moifying slightly example from help file I tried: , For a paired plot, you want to nest the idx keyword option as such:
code :
paired_mean_diff <- 
  dabest(meandiff, x = Group, y = Measurement,
    idx = list(c("Control1", "Test1"), 
               c("Control2", "Test2")),
    paired = TRUE,
    id.col = ID)


Share : facebook icon twitter icon

SELECT values from table with grouped id but differences in non-grouped columns


By : rajasekaran
Date : March 29 2020, 07:55 AM
like below fixes the issue I'm having trouble achieving something that seems like it should be simple. The example below shows people enrolled in classes, and a person can be enrolled in multiple classes, but only one currently: , I think it's a lot simpler than previously tried methods:
code :
SELECT Test.*
FROM @Test Test
JOIN
    (
        SELECT PersonID, LocationID
        FROM @Test T
        WHERE ISENROLLED = 1
        GROUP BY PeronID, LocationID
    ) T
    ON Test.PersonID = T.PersonID
    AND Test.LocationID = T.LocationID

r - ggplot2 - Add differences to grouped bar charts


By : myfairsyer
Date : March 29 2020, 07:55 AM
Any of those help As rawr suggested, you can add a layer of bars behind the current ones with a slightly smaller width:
code :
library(tidyverse)
diff_df = DF %>%
    group_by(variable) %>%
    spread(Type, value) %>%
    mutate(diff = Simulated - Observed)

ggplot(DF, aes(variable, value)) +
    geom_bar(aes(y = Simulated), data = diff_df, stat = "identity", fill = "grey80", width = 0.4) +
    geom_bar(aes(fill = Type), position = "dodge", stat="identity", width=.5) +
    geom_text(aes(label=value, group=Type), position=position_dodge(width=0.5), vjust=-0.5) +
    geom_text(aes(label = diff, y = Simulated), vjust=-0.5, data = diff_df, hjust = 2, colour = scales::muted("red")) +
    theme_bw(base_size = 18) +
    ylab('Duration (days)') + xlab('Growth stages')
library(tidyverse)
diff_df = DF %>%
    group_by(variable) %>%
    spread(Type, value) %>%
    mutate(diff = Simulated - Observed,
           max_y = max(Simulated, Observed),
           sim_higher = Simulated > Observed)

ggplot(DF, aes(variable, value)) +
    geom_bar(aes(y = max_y), data = diff_df, stat = "identity", fill = "grey80", width = 0.4) +
    geom_bar(aes(fill = Type), position = "dodge", stat="identity", width=.5) +
    geom_text(aes(label=value, group=Type), position=position_dodge(width=0.5), vjust=-0.5) +
    geom_text(aes(label = diff, y = max_y), vjust=-0.5, data = diff_df %>% filter(sim_higher), 
              hjust = 2, colour = scales::muted("red")) +
    geom_text(aes(label = diff, y = max_y), vjust=-0.5, data = diff_df %>% filter(!sim_higher), 
              hjust = -1, colour = scales::muted("red")) +
    theme_bw(base_size = 18) +
    ylab('Duration (days)') + xlab('Growth stages')

The dplyr way to get grouped differences


By : Kane Ki
Date : March 29 2020, 07:55 AM
it should still fix some issue I am trying to figure out the dplyr way to do grouped differences. , If you don't have missing years for each week:
code :
df %>% 
    arrange(year) %>%
    group_by(week) %>% 
    mutate(diffs = value - lead(value)) %>% 
    na.omit() %>% select(-value)

# A tibble: 9 x 3
# Groups:   week [3]
#   year  week diffs
#  <int> <int> <dbl>
#1     1     1     1
#2     1     2     1
#3     1     3     1
#4     2     1     1
#5     2     2     1
#6     2     3     1
#7     3     1     1
#8     3     2     1
#9     3     3     1

Multiple grouped differences in dplyr


By : Augustinas Kunsmonas
Date : March 29 2020, 07:55 AM
hope this fix your issue Goal , First, you can get the differences using another group_by.
code :
diffs <- df %>% 
  group_by(year, period) %>% 
  summarise(mean = mean(n)) %>%
  group_by(year) %>%
  summarise(diff = diff(mean))

# A tibble: 2 x 2
   year  diff
  <dbl> <dbl>
1  2016   599
2  2017   105
diff(rev(diffs$diff))

[1] 493.8846
m1 <- lm(n ~ period + factor(year) + period*factor(year), data = df)
summary(m1)

r - How to Create loop for plotting in dabestr?


By : user3461260
Date : March 29 2020, 07:55 AM
I hope this helps . The issue is not with the for statement, it's with the dabest function. It's made to only accept column names as given in .data so a string with the column name doesn't work...
After digging a bit, I found this answer very helpful for dplyr related problems with variable names.
code :
library(dabestr)
library(ggpubr) # for ggarrange

to_plot <- c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")
plots <- lapply(to_plot, function(co){
  plot(dabest(iris, Species, UQ(rlang::sym(co)), idx = c("setosa", "versicolor"), paired = FALSE))
})

ggarrange(plotlist = plots, nrow = 2, ncol = 2)
Related Posts Related Posts :
  • In R, is growing a list just as inefficient as growing a vector?
  • Flexdashboard, rhandsontable: how to programmatically access user updated table?
  • Creating Summary Table from R Variables
  • Average over groups and include previous groups
  • R: data.table count rows on specific columns > 0
  • Transform (shuffle) just 2 Fields in a Dataframe
  • Issue with replacing string by match in R
  • (very) Simple quantstrat trading model using logistic regression
  • R - count maximum number of consecutive dates
  • Problems using tidyr separate on "|"
  • Default value when calling a function in a for loop
  • Finding values in a matrix from list of values in R
  • count 0's in a zoo (or dataframe) object
  • Finding the first non-zero year in data frame for multiple variables using tidyverse
  • ggplot2 - how to assign geom_text with arrow icon to second yaxis scale
  • regex fails with dollar sign
  • Drop first element of list of lists, condense list of lists? Too many elements?
  • R - how to apply output of ifelse(str_detect ...) to whole group
  • caret package confusion matrix define positive case with multiple classes
  • Generating a pairwise 'distance' matrix
  • Change all R columns names using a reference file
  • Exclude or set a unique color to the bottom triangle of a correlation matrix heatmap
  • r shiny observe function clears text input
  • Split column by multiple delimiters, keeping delimiters
  • How to random search in a specified grid in caret package?
  • merge 2 data frames in a loop for each column in one of them
  • how to edit the codes for the summary of R S4 Object?
  • Remove specific rows in R
  • Flatten JSON list into data frame
  • Filtering a dataset and making a ggplot
  • Align cells vertically to be at the bottom flextable
  • R speed up sapply
  • invalid subscript type 'list' Azure Machine Learning
  • Use rollapply with xts object and an anonymous defined function
  • Isolate data frames from a spreadsheet to create a list
  • Error in xts, as.POSIXct "'order.by' cannot contain 'NA', 'NaN', or 'Inf'"
  • Column splitting in R
  • number similar/duplicated rows in R
  • Count the number of times each value appears in a row dataframe r
  • how to vectorise my code in r using for loop?
  • A function to fill in a column with NA of the same type
  • Network flow balancing constraint in R
  • Adding main titles from list to graphs in for loop
  • create a matrix in Perl or R if data is provided in CSV file
  • Passing column names as string to with
  • R - filtering rows and summing
  • How to change the order of fill aesthetic in faceted ggplot?
  • Function to remove outliers by group from dataframe
  • How to find the difference of max & min values in one group in a variable in a dataframe
  • Convert unicode to a readable string
  • Wrong scale/difficult to interpret times on time series object using 'ts'
  • Joining three numeric columns without adding them in r
  • Is there any way to extract the names of columns from an excel sheet without actually loading the sheet into the RAM?
  • case_when() not working: Error in mutate_impl(.data, dots)
  • Hide boxes if input not suitable in Shiny
  • Make nodes as images in R with visNetwork lib
  • Re-shape status columns based on value in another column
  • Why does the plot size differ between docx and html in rmarkdown::render?
  • Reverse x-axis that contains categorical data and a lot of annotations
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org