Home > Blockchain >  Adjustment for graph generation in R
Adjustment for graph generation in R

Time:10-02

Could you help me plot the graph corresponding to the CDE code? I can do it for ABC but not for CDE. From what I saw it is due to my variable datas <- datas[dif:max(datas$Days, na.rm = TRUE),], which is selected lines 3 to 6 of Code ABC and excluding the others. However, to generate the CDE graph lines 3 to 6 need to be selected corresponding to the CDE, however, I was not able to do this in the code.

library(dplyr)
library(tidyverse)
library(lubridate)

df1 <- structure(
  list(date1 = c("2021-06-28","2021-06-28","2021-06-28","2021-06-28"),
       date2 = c("2021-06-30","2021-06-30","2021-07-01","2021-07-01"),
       Code = c("ABC","CDE","ABC","CDE"),
       Week= c("Friday","Friday","Thursday","Thursday"),
       DR1 = c(11,17,14,12),
       DR01 = c(14,11,13,12), DR02= c(14,14,16,17),DR03= c(19,17,18,12),
       DR04 = c(11,14,13,13),DR05 = c(12,11,11,11),DR06 = c(14,13,12,11)),
  class = "data.frame", row.names = c(NA, -4L))

dmda<-"2021-06-30"

x<-df1 %>% select(starts_with("DR0"))

x<-cbind(df1, setNames(df1$DR1 - x, paste0(names(x), "_PV")))
PV<-select(x, date2,Week, Code, DR1, ends_with("PV"))

med<-PV %>%
  group_by(Code,Week) %>%
  summarize(across(ends_with("PV"), median))

SPV<-df1%>%
  inner_join(med, by = c('Code', 'Week')) %>%
  mutate(across(matches("^DR0\\d $"), ~.x   
                  get(paste0(cur_column(), '_PV')),
                .names = '{col}_{col}_PV')) %>%
  select(date1:Code, DR01_DR01_PV:last_col())

SPV<-data.frame(SPV)

datas <- SPV %>%
  filter(date2 == ymd(dmda)) %>% 
  group_by(Code) %>%
  summarize(across(starts_with("DR0"), sum)) %>%
  pivot_longer(cols = -Code, names_pattern = "DR0(. )", 
               values_to = "val") %>% 
  mutate(name = readr::parse_number(name))
colnames(datas)[-1] <-c("Days","Numbers")

dif <- as.Date(dmda) - as.Date(df1$date1[1])   1

datas <- datas[dif:max(datas$Days, na.rm = TRUE),]
> datas
# A tibble: 4 x 3
  Code   Days Numbers
  <chr> <dbl>   <dbl>
1 ABC       3      11
2 ABC       4      11
3 ABC       5      11
4 ABC       6      11 


f1 <- function(dat, code_nm) {
  dat <- subset(dat,  Code == code_nm)
  
  max<-max(datas$Days, na.rm = TRUE) 1
  
  dif <- as.Date(dmda) - as.Date(df1$date1[1])   1
  
  datas<-datas[dif:max,]
  
  plot(Numbers ~ Days,  xlim= c(0,7), ylim= c(0,30),
       xaxs='i',data = datas,main = paste0(dmda, "-", code_nm))
  
  model <- nls(Numbers ~ b1*Days^2 b2,start = list(b1 = 0,b2 = 0),data = dat, algorithm = "port")
  
  new.data <- data.frame(Days = with(dat, seq(min(Days),max(Days),len = 45)))
  new.data <- rbind(0, new.data)
  lines(new.data$Days,predict(model,newdata = new.data),lwd=2)
  coef<-coef(model)[2]
  points(0, coef, col="red",pch=19,cex = 2,xpd=TRUE)
  text(.99,coef   1,round(coef,1), cex=1.1,pos=4,offset =1,col="black")
}

f1(datas, "ABC")

enter image description here

f1(datas, "CDE")
> f1(datas, "CDE")
Error in if (any(nEQ <- vNms != make.names(vNms))) vNms[nEQ] <- paste0("`",  : 
  missing value where TRUE/FALSE needed

To generate graph for CDE, datas variable will need to be:

> datas
# A tibble: 12 x 3
   Code   Days Numbers
   <chr> <dbl>   <dbl>
 9 CDE       3      17
10 CDE       4      17
11 CDE       5      17
12 CDE       6      17

CodePudding user response:

The subsetting by the difference of dates should also consider by 'Code'

library(dplyr)
datas <- datas %>% 
     group_by(Code) %>% 
      slice((as.Date(dmda) - min(as.Date(df1$date1)[
           df1$Code == first(Code)])   1):max(Days)) %>%
     ungroup

Some of the steps are already done outside the function, remove it and also don't specify 'datas' which is the full data

f1 <- function(dat, code_nm) {
  dat <- subset(dat,  Code == code_nm)
  
  
  
  plot(Numbers ~ Days,  xlim= c(0,7), ylim= c(0,30),
       xaxs='i',data = dat,main = paste0(dmda, "-", code_nm))
  
  model <- nls(Numbers ~ b1*Days^2 b2,start = list(b1 = 0,b2 = 0),data = dat, algorithm = "port")
  
  new.data <- data.frame(Days = with(dat, seq(min(Days),max(Days),len = 45)))
  new.data <- rbind(0, new.data)
  lines(new.data$Days,predict(model,newdata = new.data),lwd=2)
  coef<-coef(model)[2]
  points(0, coef, col="red",pch=19,cex = 2,xpd=TRUE)
  text(.99,coef   1,round(coef,1), cex=1.1,pos=4,offset =1,col="black")
}

-testing

> f1(datas, "CDE")

-output enter image description here

> f1(datas, "ABC")

-output

enter image description here

  • Related