Home > Blockchain >  Is there a method of calculating SD, for 4 different veriables, of their repeats using R
Is there a method of calculating SD, for 4 different veriables, of their repeats using R

Time:04-01

A1, B1, C1 are all repeats of experiment 1. A2, B2, C2 are repeats of experiment 2 and so on. I have been attempting to SD the three points during the corresponding time point to create error bars. The code I have worked on. I have tried to mutate, summarise and gather but I must be doing something wrong.

Code so far:

Data2 <- select(Data1, Time = c(1), x= c(2:4), y = c(5:7), a = c(8:10), b = c(11:13)) 
SD = 
Data3 <- Data2 %>%
mutate(SD = sd(unlist(select(cur_data(), x1:x3))))   
  gather(key = "test", value = "value", c(-Time)) %>%
group_by(Data2, Time,  c(2:4), c(5:7), c(8:10), c(11:13)) %>%
  summarise(sd = sd(value, na.rm=F))
  ggplot(aes(x = Time, y = value, colour= test))  
  geom_point(aes(col = test), alpha = 1)  
  geom_smooth(aes(col = test, method = "lm"))  
  geom_errorbar(aes(ymin=value-sd, ymax=value sd), width=5, size = 0.5) 
  facet_wrap(.~test, ncol = 2) 
  theme_classic() 
  xlab("Time (hours)") 
  ylab("Weight (grams)")
  legend(=null)

Like I said nothing has worked and I've been experimenting for the last 3 days. No idea where I went wrong or what was is forward at this point.

 group_by(Time) %>%
  summarise(sd= sd(value, na.rm=T))

this code came the closest to working but SD all of my experiments, not the three repeats.

Thanks in advance.

Data:

structure(list(Time = c(0, 24, 48, 72, 96, 120, 142), A1 = c(7.687, 
NA, NA, 7.687, 7.738, 7.796, 7.747), B1 = c(7.661, 7.68, 7.694, 
7.728, 7.707, NA, NA), C1 = c(7.684, 7.991, 8.027, 7.978, 7.966, 
NA, NA), A2 = c(7.671, NA, NA, 7.684, 7.679, 7.731, 7.69), B2 = c(7.672, 
7.85, 7.838, 7.835, 7.769, NA, NA), C2 = c(7.67, 7.906, 8.02, 
8.032, 8.04, NA, NA), A3 = c(7.678, NA, NA, 7.794, 7.731, 7.817, 
7.862), B3 = c(7.689, 7.882, 7.937, 7.937, 7.936, NA, NA), C3 = c(7.68, 
7.975, 8.003, 7.971, 7.971, NA, NA), A4 = c(7.703, NA, NA, 7.714, 
7.752, 7.737, 7.793), B4 = c(7.703, 7.916, 7.992, 7.991, 7.964, 
NA, NA), C4 = c(7.706, 7.839, 8.384, 8.391, 8.397, NA, NA)), row.names = c(NA, 
-7L), class = c("tbl_df", "tbl", "data.frame"))

I'm looking for something like this image. I just plotted two data sets out (the long way) but I have hundreds of these and it would take me a year.

CodePudding user response:

not sure what output you are expecting (perhaps include a small sketch in your question?)

here is a first try/approach

library(tidyverse)
Data1 %>%
  pivot_longer(
    cols = -1,
    names_to = c("try", "exp"),
    names_pattern = "(.)(.)") %>%
  ggplot(aes(x = Time, y = value))   
  geom_point()   
  stat_summary(func = "sd", 
               geom = "errorbar")   
  facet_grid(~exp)

enter image description here

  • Related