Data and Packages

ERPdata (from erpR)
library(ERP);require(mnormt);require(fdrtool);library(ggplot2);library(dplyr);library(gridExtra)
library(erpR);require(akima);library(reshape2);library(boot);library(plotly);library(ggmap);library(SCBmeanfd)
load("ERPdata.RData")

Single Function : fdaplot

It could draw several plots

1. Raw data plot
2. Average Signal Plot
3. Bootstraping Interval Plot
4. test plot
5. test plot with signal
# "fdaplot" Code
fdaplot <- function(data,  # The ERP or NIRS data
                    frames = NULL, # The time point of the data
                    data.var , # The column of "Data"
                    subj.var = NULL, # The column of "Subject"
                    chan.var = NULL,  # The column of "Channel"
                    test.var = NULL, # The column of the variable that you want to compare (e.g. "Condition")
                    subj.select = NULL, # The Subject you want to select
                    chan.select = NULL, # The Channels you want to select
                    type = c("raw","average","boot.ci","test","test_signal"), # five types of the possible plot 
                    curve.col = c("blue","red"), # the color for the curve (e.g. two conditions need two colors)
                    labs = list(x = "Frames", y = "Signal"), # Labs of the plot
                    scalp = FALSE, # Do you want to plot on the scalp ?
                    ylim = c(20,-20), # y limits of the plot
                    curve.fun = function(x, d){return(mean(x[d]))}, # when  type == average or bootci
                    boot.num = 500 ,    # when type == bootci, the bootstraping number 
                    boot.intval = c(0.025,0.975), # when type == bootci, the bootstraping interval
                    ci.alpha = 0.3, # when type == bootci, the alpha value of the ribbon interval
                    cor = F, # use in correlation settings (not yet)
                    significant.col = "pink", # when type ==  test | type = test_signal, the color of the significant window
                    significant.alpha = 0.2, # when type ==  test | type = test_signal, the alpha value of the significant window
                    # the argument for erpfatest
                    design = NULL, design0 = NULL,...){ 
        # Some Check Functions (need More)
        if (!is.null(test.var)){
                if (length((levels(as.factor(data[,test.var])))) != length(curve.col)){
                        stop("curve.col should be equal to levels of test.var !")
                }
        }
        if (is.null(frames)){frames  = 1:length(data.var)}
        # the function of selecting data
        data_select <- function(data,data.var , subj.var = NULL, chan.var = NULL,
                                subj.select = NULL,chan.select = NULL){
                dta <- data 
                if (is.null(subj.select) == FALSE & is.null(chan.select) == FALSE ) {
                        dta_s <- subset(dta,dta[,chan.var] %in% chan.select & dta[,subj.var] %in% subj.select) 
                } else if (is.null(subj.select) == FALSE & is.null(chan.select) == TRUE) {
                        dta_s <- subset(dta,dta[,subj.var] %in% subj.select ) 
                } else if (is.null(subj.select) == TRUE & is.null(chan.select) == FALSE) {
                        dta_s <- subset(dta,dta[,chan.var] %in% chan.select) 
                } else {
                        dta_s <- dta
                }
                dta_s[,chan.var] <- as.factor(as.character(dta_s[,chan.var]))
                dta_s[,subj.var] <- as.factor(as.character(dta_s[,subj.var]))
                return (dta_s)
        }
        # Select the data
        dta <- data_select(data, data.var, subj.var, chan.var,subj.select,chan.select)
        # Some plot functions
        edaplot <- function(data,frames=NULL,data.var,subj.var=NULL,chan.var=NULL){
                colnames(data)[chan.var] <- "Channel"
                oth.var = (1:dim(data)[2]) [! 1:dim(data)[2] %in% c(data.var,subj.var,chan.var)]
                data$Info <- NA
                for (i in 1 : dim(data)[1]){
                        info1 <- paste0(as.character(data[i,oth.var]),collapse=";")
                        data$Info[i] <- paste(data[i,chan.var],data[i,subj.var],info1,sep=";")
                }
                datalong <- melt(data,
                                 id=c(variable.names(data)[c(subj.var,chan.var,oth.var)],
                                      "Info"))
                datalong <- datalong[order(datalong$Info),]
                datalong$frames <- rep(frames,length(datalong[,1])/length(frames))
                plot <- ggplot(datalong,aes(x=frames,y=value,group=Info))+
                        geom_line()
                return(plot)
        }
        # The test function
        chan_test <- function(data,data.var = NULL,chan.var = NULL,design_model,design0_model=NULL,...){
                dta <- data
                levelnum <- length(levels(dta[,chan.var]))
                if (levelnum == 1) {
                        design <- model.matrix(design_model,data=data)
                        if (is.null(design0_model)==F){
                                design0 <- model.matrix(design0_model,data=data)
                        }
                        test_list <- erpfatest(dta[,data.var],design,design0,...)
                } else {
                        test_list=list()
                        dta_list=list()
                        for (i in 1:levelnum) {
                                dta_list[[i]] <- subset(dta,dta[,chan.var]==(levels(dta[,chan.var])[i]))
                                design <- model.matrix(design_model,data=dta_list[[i]])
                                if (is.null(design0_model)==F){
                                        design0 <- model.matrix(design0_model,data=dta_list[[i]])
                                }
                                test_list[[i]] <- erpfatest(dta_list[[i]][,data.var],design,design0,...)
                                names(test_list)[i] <- levels(dta[,chan.var])[i]
                        }
                }
                return(test_list)
        }
        # Produce the test results called "tests_rst", when type  == "test" or "test_signal"
        if (type == "test" | type == "test_signal"){tests_rst <- chan_test(dta,data.var,chan.var,...)}
        # The summarize function
        data_summarize <- function(data,data.var,summary.var,fun=mean,...){ 
                dta <- data
                options(warn=-1)
                agglength <- length(summary.var)
                aggvar_list <- list(dta[,summary.var[1]])
                if (agglength > 1){
                        for (i in 2:agglength ){
                                aggvar_list <- append(aggvar_list,list(dta[,summary.var[2]]))
                        }
                }
                aggdata <- aggregate(dta[,data.var],by=aggvar_list,
                                     fun,...)
                aggdata <- aggdata[,1:(agglength+length(data.var))]
                for (i in 1: agglength){
                        colnames(aggdata)[i] <- colnames(dta)[summary.var[i]]
                }
                rownames(aggdata) <- 1:dim(aggdata)[1] 
                return(aggdata)
        }
        # The bootstraping function
        bootstrap <- function(x,bootnum,bootfun,bootintval=c(0.025,0.975),quantilenum,...){
                boot_result <- boot(x,statistic = bootfun,R = bootnum,...)
                return(quantile(boot_result$t,bootintval,na.rm = T)[quantilenum])
        }
        # Produce the data of confidence interval
        if (type == "bootci"){
                if (is.null(test.var)==TRUE){
                        data_FUN <- data_summarize(dta,data.var,summary.var = chan.var,fun = curve.fun)
                        data_Q1 <- data_summarize(dta,data.var,summary.var = chan.var,
                                                  fun=bootstrap,bootnum=boot.num,bootfun=curve.fun,
                                                  bootintval=boot.intval,quantilenum=1)
                        data_Q2 <- data_summarize(dta,data.var,summary.var = chan.var,
                                                  fun=bootstrap,bootnum=boot.num,bootfun=curve.fun,
                                                  bootintval=boot.intval,quantilenum=2)
                        data_fun_long <- melt(data_FUN,id=c(colnames(dta)[chan.var]))
                        colnames(data_fun_long)[3] <- "FUN"
                        data_Q1_long <- melt(data_Q1,id=c(colnames(dta)[chan.var]))
                        colnames(data_Q1_long)[3] <- "Q1"
                        data_Q2_long <- melt(data_Q2,id=c(colnames(dta)[chan.var]))
                        colnames(data_Q2_long)[3] <- "Q2"
                        data_ci <- full_join(data_fun_long,data_Q1_long,by = c(colnames(dta)[chan.var],"variable"))
                        data_ci <- full_join(data_ci,data_Q2_long,by = c(colnames(dta)[chan.var],"variable"))
                        colnames(data_ci)[1] <- c("Channel")
                } 
                else {
                        dta[,test.var] <- as.factor(dta[,test.var])
                        dta[,chan.var] <- as.factor(dta[,chan.var])
                        data_FUN <- data_summarize(dta,data.var,summary.var = c(chan.var,test.var),fun = curve.fun)
                        data_Q1 <- data_summarize(dta,data.var,summary.var = c(chan.var,test.var),
                                                  fun=bootstrap,bootnum=boot.num,bootfun=curve.fun,
                                                  bootintval=boot.intval,quantilenum=1)
                        data_Q2 <- data_summarize(dta,data.var,summary.var = c(chan.var,test.var),
                                                  fun=bootstrap,bootnum=boot.num,bootfun=curve.fun,
                                                  bootintval=boot.intval,quantilenum=2)
                        data_fun_long <- melt(data_FUN,id=c(colnames(dta)[chan.var],colnames(dta)[test.var]))
                        colnames(data_fun_long)[c(1,2,4)] <- c("Channel","Condition","FUN")
                        data_Q1_long <- melt(data_Q1,id=c(colnames(dta)[chan.var],colnames(dta)[test.var]))
                        colnames(data_Q1_long)[c(1,2,4)] <- c("Channel","Condition","Q1")
                        data_Q2_long <- melt(data_Q2,id=c(colnames(dta)[chan.var],colnames(dta)[test.var]))
                        colnames(data_Q2_long)[c(1,2,4)] <- c("Channel","Condition","Q2")
                        data_ci <- full_join(data_fun_long,data_Q1_long,by = c("Channel","Condition","variable"))
                        data_ci <- full_join(data_ci,data_Q2_long,by = c("Channel","Condition","variable"))
                } 
        }
        # Draw the plot (not on 10/10 system)
        if (scalp == F){
                if (type == "raw"){
                        data_plot <- dta
                        colnames(data_plot)[chan.var] <- "Channel"
                        if (is.null(test.var)){
                                plot <- edaplot(data = data_plot,frames = frames,data.var = data.var,subj.var=subj.var,
                                                chan.var=chan.var)+
                                        geom_line(col = curve.col[1])+
                                        facet_wrap(~Channel)+
                                        labs(labs)+
                                        ylim(ylim)+
                                        theme_bw()
                        } 
                        else {
                                colnames(data_plot)[test.var] <- "Condition"
                                values <- curve.col[1]
                                for (i in 2:length(levels(as.factor(dta[,test.var])))){
                                        values <- c(values,curve.col[i])
                                }
                                plot <- edaplot(data = data_plot,frames = frames,data.var = data.var,subj.var=subj.var,
                                                chan.var=chan.var)+
                                        geom_line(aes(col = Condition))+
                                        facet_wrap(~Channel)+
                                        labs(labs)+
                                        scale_color_manual(values = values, name = colnames(dta)[test.var])+
                                        ylim(ylim)+
                                        theme_bw()
                        }
                }
                if (type == "average") {
                        if (is.null(test.var)){
                                data_plot <- data_summarize(dta,data.var ,summary.var = chan.var,fun = curve.fun)
                                data_plot <- melt(data_plot)
                                colnames(data_plot)[1] <- "Channel"
                                data_plot <- data_plot[order(data_plot$Channel,data_plot$variable),]
                                data_plot$frames <- rep(frames,dim(data_plot)[1]/length(frames))
                                plot <- ggplot(data = data_plot,aes(x = frames, y = value))+
                                        geom_line(col = curve.col[1])+
                                        facet_wrap(~Channel)+
                                        theme_bw()+
                                        labs(labs)+
                                        ylim(ylim)+
                                        theme_bw()
                        } 
                        else {
                                values <- curve.col[1]
                                for (i in 2:length(levels(as.factor(dta[,test.var])))){
                                        values <- c(values,curve.col[i])
                                }
                                data_plot <- data_summarize(dta,data.var ,summary.var = c(chan.var,test.var),fun = curve.fun)
                                data_plot <- melt(data_plot)
                                colnames(data_plot)[c(1,2)] <- c("Channel","Condition")
                                data_plot <- data_plot[order(data_plot$Channel,data_plot$Condition,data_plot$variable),]
                                data_plot$frames <- rep(frames,dim(data_plot)[1]/length(frames))
                                plot <- ggplot(data = data_plot,aes(x = frames, y = value))+
                                        geom_line(aes(col = Condition))+
                                        facet_wrap(~Channel)+
                                        theme_bw()+
                                        labs(labs)+
                                        scale_color_manual(values = values, name = colnames(dta)[test.var])+
                                        ylim(ylim)+
                                        theme_bw()
                        }
                } 
                if (type == "bootci"){
                        if (is.null(test.var)){
                                data_ci <- data_ci[order(data_ci$Channel),]
                                data_ci$frames <- rep(frames,dim(data_ci)[1]/length(frames))
                                plot <- ggplot(data = data_ci,aes(x = frames, y = FUN))+
                                        geom_ribbon(aes(ymax = Q1,ymin = Q2),alpha = ci.alpha, fill = curve.col[1])+
                                        geom_line(col = curve.col[1])+
                                        facet_wrap(~Channel)+
                                        theme_bw()+
                                        labs(labs)+
                                        ylim(ylim)+
                                        theme_bw()
                        } 
                        else {
                                values <- curve.col[1]
                                for (i in 2:length(levels(as.factor(dta[,test.var])))){
                                        values <- c(values,curve.col[i])
                                }
                                data_ci <- data_ci[order(data_ci$Channel,data_ci$Condition,data_ci$variable),]
                                data_ci$frames <- rep(frames,dim(data_ci)[1]/length(frames))
                                plot <- ggplot(data = data_ci,aes(x = frames, y = FUN))+
                                        geom_ribbon(aes(ymax = Q2,ymin=Q1, fill = Condition),alpha = ci.alpha)+
                                        geom_line(aes(col = Condition))+
                                        facet_wrap(~Channel)+
                                        theme_bw()+
                                        labs(labs)+
                                        scale_color_manual(values = values ,
                                                           name = colnames(dta)[test.var])+
                                        scale_fill_manual(values = values ,
                                                          name = colnames(dta)[test.var])+
                                        ylim(ylim)+
                                        theme_bw()
                        }  
                }
                if (type == "test") {
                        if (length(levels(dta[,chan.var]))==1){
                                data_test <- data.frame(signal=as.numeric(tests_rst$signal))
                                data_test$frames = frames
                                data_test$significant <- 
                                        ifelse(data_test $frames %in% data_test $frames[tests_rst$significant],
                                               "sig","non-sig")
                                data_test$sign_frames <- 
                                        ifelse(data_test $frames %in% data_test$frames[tests_rst$significant],
                                               data_test $frames,NA)
                                data_test $group <- rep(0,length(data_test $signal))
                                data_test $r2 <- tests_rst$r2
                                if (cor == TRUE){
                                        plot <- ggplot(data_test ,
                                                       aes(x=frames,y=sign(signal)*sqrt(r2),group=group))+
                                                geom_vline(data=data_test ,
                                                           aes(xintercept = sign_frames,
                                                               col=significant.col),
                                                           alpha=significant.alpha)+
                                                geom_line()+
                                                labs(labs)+
                                                theme_bw()+
                                                theme(legend.position="none")+
                                                ylim(ylim)
                                } 
                                else {
                                        plot <- ggplot(data_test ,aes(x=frames,y=signal,group=group))+
                                                geom_vline(data=data_test ,
                                                           aes(xintercept = sign_frames),
                                                           col=significant.col,
                                                           alpha=significant.alpha)+
                                                geom_line()+
                                                labs(labs)+
                                                theme_bw()+
                                                theme(legend.position="none")+
                                                ylim(ylim)
                                }
                        } 
                        else {
                                listlen <- length(tests_rst)
                                data_list <- list()
                                for (k in 1:listlen){
                                        data_list[[k]] <- data.frame(signal=as.numeric(tests_rst[[k]]$signal))
                                        data_list[[k]]$frames = frames
                                        data_list[[k]]$significant<-ifelse(data_list[[k]]$frames %in% data_list[[k]]$frames[tests_rst[[k]]$significant],"sig","non-sig")
                                        data_list[[k]]$sign_frames <- ifelse(data_list[[k]]$frames %in% data_list[[k]]$frames[tests_rst[[k]]$significant],data_list[[k]]$frames,NA)
                                        data_list[[k]]$group <- rep(0,length(data_list[[k]]$signal))
                                        data_list[[k]]$r2 <- tests_rst[[k]]$r2
                                        data_list[[k]]$Channel <- names(tests_rst)[k]
                                }
                                data_plot <- data_list[[1]]
                                for (j in 2 : listlen){
                                        data_plot <- rbind(data_plot,data_list[[j]])
                                }
                                if (cor == TRUE){
                                        plot <- ggplot(data_plot,aes(x=frames,y=sign(signal)*sqrt(r2),group=group))+
                                                geom_vline(data=data_plot,
                                                           aes(xintercept = sign_frames),
                                                           col=significant.col,
                                                           alpha=significant.alpha)+
                                                geom_line()+
                                                facet_wrap(~Channel)+
                                                labs(labs)+
                                                theme_bw()+
                                                theme(legend.position="none")+
                                                ylim(ylim)
                                } 
                                else {
                                        plot <- ggplot(data_plot,aes(x=frames,y=signal,group=group))+
                                                geom_vline(data=data_plot,
                                                           aes(xintercept = sign_frames),
                                                           col=significant.col,
                                                           alpha=significant.alpha)+
                                                geom_line()+
                                                labs(labs)+
                                                facet_wrap(~Channel)+
                                                theme_bw()+
                                                theme(legend.position="none")+
                                                ylim(ylim)
                                }
                        }
                }
                if (type == "test_signal"){
                        if (length(levels(dta[,chan.var]))==1){
                                if (is.null(test.var)){
                                        data_sign <- data.frame(frames=frames)
                                        data_sign$significant <- 
                                                ifelse(data_sign$frames %in% data_sign$frames[tests_rst$significant],"sig","non-sig")
                                        data_sign$sign_frames <- 
                                                ifelse(data_sign$frames %in% data_sign$frames[tests_rst$significant],data_sign$frames,NA)
                                        data_sign$group <- rep(0,length(frames))
                                        data_plot <- data_summarize(dta,data.var ,summary.var = chan.var,fun = curve.fun)
                                        data_plot <- melt(data_plot)
                                        colnames(data_plot)[1] <- "Channel"
                                        data_plot <- data_plot[order(data_plot$Channel,data_plot$variable),]
                                        data_plot$frames <- rep(frames,dim(data_plot)[1]/length(frames))
                                        plot <- ggplot(data = data_plot,aes(x = frames, y = value))+
                                                geom_line(col = curve.col[1])+
                                                geom_vline(data = data_sign,aes(xintercept = sign_frames),
                                                           col = significant.col, alpha =  significant.alpha)+
                                                facet_wrap(~Channel)+
                                                theme_bw()+
                                                labs(labs)+
                                                ylim(ylim)+
                                                theme_bw()
                                        
                                }
                                else {
                                        values <- curve.col[1]
                                        for (i in 2:length(levels(as.factor(dta[,test.var])))){
                                                values <- c(values,curve.col[i])
                                        }
                                        data_sign <- data.frame(frames=frames)
                                        data_sign$significant <- 
                                                ifelse(data_sign$frames %in% data_sign$frames[tests_rst$significant],"sig","non-sig")
                                        data_sign$sign_frames <- 
                                                ifelse(data_sign$frames %in% data_sign$frames[tests_rst$significant],data_sign$frames,NA)
                                        data_sign$group <- rep(0,length(frames))
                                        data_plot <- data_summarize(dta,data.var ,summary.var = c(chan.var,test.var),fun = curve.fun)
                                        data_plot <- melt(data_plot)
                                        colnames(data_plot)[1:2] <- c("Channel","Condition")
                                        data_plot <- data_plot[order(data_plot$Channel,data_plot$Condition,data_plot$variable),]
                                        data_plot$frames <- rep(frames,dim(data_plot)[1]/length(frames))
                                        plot <- ggplot(data = data_plot,aes(x = frames, y = value))+
                                                geom_line(aes(col=Condition))+
                                                geom_vline(data = data_sign,aes(xintercept = sign_frames),
                                                           col = significant.col, alpha =  significant.alpha)+
                                                facet_wrap(~Channel)+
                                                labs(labs)+
                                                ylim(ylim)+
                                                theme_bw()+
                                                scale_color_manual(values = values,name = colnames(dta)[test.var])
                                }
                        } 
                        else { 
                                listlen <- length(tests_rst)
                                test_list = list()
                                for (i in 1: listlen){
                                        test_list[[i]] <- data.frame(frames=frames)
                                        test_list[[i]]$significant <- ifelse(test_list[[i]]$frames %in% test_list[[i]]$frames[tests_rst[[i]]$significant],"sig","non-sig")
                                        test_list[[i]]$sign_frames <- ifelse(test_list[[i]]$frames %in% test_list[[i]]$frames[tests_rst[[i]]$significant],test_list[[i]]$frames,NA)
                                        test_list[[i]]$group <- rep(0,length(frames))
                                        test_list[[i]]$Channel <- names(tests_rst)[i]
                                }
                                test_plot <- test_list[[1]]
                                for (k in 2:listlen){
                                        test_plot <- rbind(test_plot,test_list[[k]])
                                }
                                if (is.null(test.var)){
                                        data_fun <- data_summarize(data  = dta,data.var = data.var,
                                                                   summary.var=chan.var,fun=curve.fun)
                                        data_fun_long <- melt(data_fun,id=c(colnames(dta)[chan.var]))
                                        data_fun_long <- data_fun_long[order(data_fun_long[,1],
                                                                             data_fun_long[,2],
                                                                             data_fun_long[,3]),]
                                        colnames(data_fun_long)[3] <- "FUN"
                                        colnames(data_fun_long)[1] <- c("Channel")
                                        data_fun_long$frames <- rep(frames,dim(data_fun_long)[1]/length(frames))
                                        data_plot <- merge(data_fun_long,
                                                           test_plot,
                                                           by =c("Channel","frames"))
                                        plot <- ggplot(data_plot,aes(x=frames))+
                                                geom_vline(aes(xintercept = sign_frames),
                                                           col=significant.col,
                                                           alpha=significant.alpha)+
                                                geom_line(aes(y = FUN),col = curve.col[1])+
                                                facet_wrap(~Channel)+
                                                ylim(ylim)+
                                                labs(labs)+
                                                theme_bw()
                                } 
                                else {
                                        dta[,test.var] <- as.factor(dta[,test.var])
                                        values <- curve.col[1]
                                        for (i in 2:length(levels(as.factor(dta[,test.var])))){
                                                values <- c(values,curve.col[i])
                                        }
                                        data_fun <- data_summarize(dta,data.var = data.var,summary.var = c(chan.var,test.var),fun=curve.fun)
                                        data_fun_long <- melt(data_fun,id=c(colnames(dta)[chan.var],colnames(dta)[test.var]))
                                        data_fun_long <- data_fun_long[order(data_fun_long[,1],data_fun_long[,2],data_fun_long[,3]),]
                                        colnames(data_fun_long)[c(1,2,4)] <- c("Channel","Condition","FUN")
                                        data_fun_long$frames <- c(rep(frames,(dim(data_fun_long)[1]/length(frames))))
                                        data_plot <- merge(data_fun_long,test_plot,by =c("Channel","frames"))
                                        plot <- ggplot(data_plot,aes(x=frames,group=Condition))+
                                                geom_vline(aes(xintercept = sign_frames),
                                                           col=significant.col,alpha=significant.alpha)+
                                                geom_line(aes(y = FUN,col=Condition))+ 
                                                labs(labs)+# Need some changes ?
                                                facet_wrap(~Channel)+
                                                labs(labs)+
                                                ylim(ylim)+
                                                theme_bw()+
                                                scale_color_manual(values = values,name = colnames(dta)[test.var])
                                }
                        }
                }
        }
        # Draw the plot ( on 10/10 system)
        if (scalp == T){} # not yet
        # if type = "test" or "test_signal", return test results and plot
        # else return only the plot (maybe we could return more things for users)
        if (type == "test" | type == "test_signal") {
                re_list <- list()
                re_list[[1]] <- plot
                re_list[[2]] <- tests_rst
                names(re_list)[1:2] <- c("Plot","Test_Rst")
        } else { re_list <- plot }
        return(re_list)
}

Examples

1. Explore the raw data

All subjects and all channels
Fig1 <- fdaplot(data = ERPdata , 
                frames = 1:426 ,
                data.var = 2:427 , 
                subj.var = 430, 
                chan.var = 1, 
                test.var = 429,
                type = "raw",
                curve.col = c("blue","red"), # indicate the color of two conditions
                ylim = c(20,-20)) 
Fig1

2. Explore the raw data

Select several subjects
Fig2 <- fdaplot(data = ERPdata , 
                frames = 1:426 ,
                data.var = 2:427 , 
                subj.var = 430, 
                chan.var = 1, 
                test.var = 429,
                subj.select = paste("subj",sample(1:20,3),sep =""), # Sample some subjects
                type = "raw",
                curve.col = c("blue","red"), # indicate the color of two conditions
                ylim = c(20,-20))
Fig2

3. Explore the raw data

On the specific channel (e.g.Fp1)
Fig3 <- fdaplot(data = ERPdata , 
                frames = 1:426 ,
                data.var = 2:427 , 
                subj.var = 430, 
                chan.var = 1, 
                test.var = 429,
                subj.select = paste("subj",sample(1:20,3),sep =""), # Sample some subjects
                chan.select = "Fp1",
                type = "raw",
                curve.col = c("blue","red"), # indicate the color of two conditions
                ylim = c(20,-20))
Fig3

4. Summarize all subjects to a curve for gaining overall information

Type == “average” , return a summarize plot
Fig4 <- fdaplot(data = ERPdata , 
                frames = 1:426 ,
                data.var = 2:427 , 
                subj.var = 430, 
                chan.var = 1, 
                test.var = 429,
                type = "average", 
                curve.col = c("green","blue"), # Change the color of the curves
                labs = list(x = "Time Points", y = "ERP Signal"), # Change the labs of the plot
                curve.fun = function(x, d){return(mean(x[d]))}, # The default function is "mean"
                ylim = c(20,-20))
Fig4

5. Summarize all subjects on a specific channel

Fig5 <- fdaplot(data = ERPdata , 
                frames = 1:426 ,
                data.var = 2:427 , 
                subj.var = 430, 
                chan.var = 1, 
                test.var = 429,
                chan.select = "Fp1",
                type = "average")

Fig5

6. Explore the comparison using bootstraping interval between two conditions

using all subjects on all channels
Fig6 <- fdaplot(data = ERPdata , 
                frames = 1:426 ,
                data.var = 2:427 , 
                subj.var = 430, 
                chan.var = 1, 
                test.var = 429,
                type = "bootci", 
                curve.fun = function(x, d){return(mean(x[d]))}, # The default function to bootstrap
                boot.num = 500 , # The times for bootstraping, default == 500
                boot.intval = c(0.025,0.975), # The confidence interval
                ci.alpha = 0.3, # The alpha of the ribbon on the plot
                ylim = c(30,-30)) # Change the y limit
Fig6

7. Explore the comparison using bootstraping interval between two conditions

using all subjects on a specific channels (e.g. Fp1)
Fig7 <- fdaplot(data = ERPdata , 
                frames = 1:426 ,
                data.var = 2:427 , 
                subj.var = 430, 
                chan.var = 1, 
                test.var = 429,
                chan.select = "Fp1",
                type = "bootci")
Fig7

8. Return the results after erpfatest, type == test or test == test_signal

Type == test, return a plot with the difference between two curve
Type == test_signal, return a plot with the original signal curve (like type == average)
But they also return the test results (a list) in the second element of the list
type == test
result <- fdaplot(data = ERPdata , 
                  frames = 1:426 ,
                  data.var = 2:427 ,
                  subj.var = 430, 
                  chan.var = 1,
                  test.var = 429,
                  type = "test",
                  significant.col = "pink", # The color of significant window
                  significant.alpha = 0.2, # The alpha value of the significant window
                  design_model=(~Subject+Condition), # Some settings abouterpfatest
                  design0_model=(~Subject)) # Some settings abouterpfatest
Fig8 <- result$Plot
Test_Results <- result$Test_Rst
Fig8

type == test_signal
result2 <- fdaplot(data = ERPdata , 
                  frames = 1:426 ,
                  data.var = 2:427 ,
                  subj.var = 430, 
                  chan.var = 1,
                  test.var = 429,
                  type = "test_signal",
                  significant.col = "pink", # The color of significant window
                  significant.alpha = 0.2, # The alpha value of the significant window
                  design_model=(~Subject+Condition), # Some settings abouterpfatest
                  design0_model=(~Subject))
Fig9 <- result2$Plot
Fig9

9. Return a test results on a specific channel (e.g. Fp1)

type == test
result_Fp1 <- fdaplot(data = ERPdata , 
                  frames = 1:426 ,
                  data.var = 2:427 ,
                  subj.var = 430, 
                  chan.var = 1,
                  test.var = 429,
                  type = "test",
                  chan.select = "Fp1",
                  significant.col = "pink", # The color of significant window
                  significant.alpha = 0.2, # The alpha value of the significant window
                  design_model=(~Subject+Condition), # Some settings abouterpfatest
                  design0_model=(~Subject)) # Some settings abouterpfatest

Fig10 <- result_Fp1$Plot
Fig10

##### type == test_signal

result2_Fp1 <- fdaplot(data = ERPdata , 
                   frames = 1:426 ,
                   data.var = 2:427 ,
                   subj.var = 430, 
                   chan.var = 1,
                   test.var = 429,
                   chan.select = "Fp1",
                   type = "test_signal",
                   significant.col = "pink", # The color of significant window
                   significant.alpha = 0.2, # The alpha value of the significant window
                   design_model=(~Subject+Condition), # Some settings abouterpfatest
                   design0_model=(~Subject))

Fig11 <- result2_Fp1$Plot
Fig11