knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(Statpackage)
#' A function for descriptive statistics
#' The first step after creating the data or reading data from an csv file is descriptive analysis.
#' This function provides the user with information such as mean, standard deviation, and etc.
#' x = rnorm (n, mean, sd)
#' y = rnorm (m, mean, sd)
#'
#' @param x vector of sample data from the population
#'
#' @return The function returns mean,median,25th and 75th quartiles,min,max
#' @export
#'
#' @examples
#' mydata(x = rnorm (20,5,10))
mydata <- function(x){
  myx <- summary(x)
  return(myx)
}
#' A function to plot x, y
#' This plot, shows the distribution, mean and standard deviation of the data in seperate box plots.
#' importFrom("graphics", "boxplot", "par")
#'
#'
#' @param x vector of sample data from population 1
#' @param y vector of sample data from population 2
#'
#' @return 2 plots, for sample one and two
#' @export
#'
#'
#' @examples
#' x = rnorm(30,10,12); y = rnorm(40,20,15); myplot(x,y)
library(ggplot2)

myplot <- function(x, y){
  plot(x, type = "p", xlab = "Sample Data of population 1", col = "dark red")
  par(new=FALSE)
  plot(y, type = "p", xlab = "Sample Data of population 2", col = "dark blue")
}
#' The box plot value
#' This function provides a discription of the box plot.
#'
#' @param x vector of sample data from population 1
#'
#' @return This function returns a list of 6 components:
#' n: the number of observation the boxplot is drawn with
#'
#' conf: upper/lower extremes of the notch, out-value of the outliers
#'
#' group: a vector of the same length that
#' indicate to which group the outlier belongs to.
#'
#' names: a vector of names for the groups.
#'
#'
#' @export
#'
#' @examples
#'  x = rnorm(30,10,12); myboxplot (x)
myboxplot <- function(x){
  xvalue <- boxplot(x, type = "p", xlab = "Sample Data of population 1",
                    col = "dark red", horizontal = TRUE)

  return(xvalue)
}
#' A function to create a violin plot to compare the two samples
#'
#' @param x : vector of sample data from population 1
#' @param y : vector of sample data from population 2
#'
#' @return A violin
#' @export
#'
#' @examples
#' x = rnorm(30,10,12); y = rnorm(40,20,15); myviolinplot(x,y)
#'
library(vioplot)
library(zoo)
myviolinplot <- function(x, y){
  title <- ("Violin plot of sample x and y")
  vioplot(x, y, names=c("Sample x", "Sample y"), main = title, col="gold")
}
#' A function for t tests
#' importFrom("stats", "rnorm", "t.test", "var.test")
#'
#'
#' The constructor function is based on t-test, that evaluates the NUll hypothesis H0.
#' H0 (The Null Hypothesis) claims that two populations have the same mean.
#'
#' x and y have underlying normal distribution.
#'
#' @param x vector of sample data from population 1
#' @param y vector of sample data from population 2
#'
#' @param paired TRUE or FALSE,
#' paired = FALSE by default
#' The samples are paired = TRUE, if each experimental unit is measured twice.
#' In that case the first experiments creates sample x,
#' and the second experiment creates sample y.
#' The two-sided t-test is used for paired sapmles.
#'
#' The test returns an error if the samples have different size.
#'
#' @param alpha alpha level is always between 0,1,
#' which determines the confidence interval for the P-value.
#'
#'
#' @return The test return a list containing the data, and t test object.
#' @export
#'
#' @examples
#' myttest(x = rnorm(30,10,12), y = rnorm(40, 7, 10))
myttest <- function(x, y, paired=FALSE, alpha=0.05){
  if(paired == "FALSE"){ #If the samples are not paired then use a t-test.
    vt = var.test(x,y)

    if(vt$p.value > alpha){
      tt <- t.test(x, y, var.equal = TRUE, conf.level = 1-alpha)
    }
    else{
      tt <- t.test(x, y, var.equal = FALSE, conf.level = 1-alpha)
    }
  }
  else{ #If the samples are paired then check the lengths.
    #If the lengths are different the algorithms stops.

    if (length(x) != length(y)){
      print("The sample sizes are not the same.")
    }
    if (length(x) == length(y)){
      tt <- t.test(x, y, paired=TRUE, conf.level=1-alpha, alternative = "two.sided")
    }

  }

  Data = c(x,y)
  v = rep(c("x","y"), c(length(x),length(y))) # Creation of qual var
  DF = data.frame(data=Data, v=v)
  lst=list(ttest=tt, paired = paired)
  class(lst) <- "mytt" #New class
  print (lst)

}


ElahehJafarigol/Statpackage documentation built on May 10, 2020, 1:45 a.m.