R/smrmul.R

#' Multivariate analysis and summary table
#'
#' Generate summary table proving the multivariate analysis for all variabls and dependent variable from the whole dataset
#'
#'
#' @param x   a regression model which generated by users
#' @param alternative	  a character string specifying the alternative approaches, must be one of "linear" (default), "logistic"
#' @param digits     the digits of the data values (default is 3)
#' @param ...	further arguments to be passed to or from methods
#'
#'
#' @return  For multivariate analysis, it returns a summary table only for significant variavbles based on the type of dependent variables. For continuous Y, the table contains parameters, coefficient, p-value, CI for coefficient; while for categorical variable, the table contains the parameters, Odds ratio, p-value, CI for odds ratio.
#'
#' @references Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
#'
#'
#' @examples
#' a<-rnorm(5,3)
#' b<-rnorm(5,7)
#' c<-cbind(a,b)
#' c<-data.frame(c)
#'
#' fit1 <- lm(a~., data = c)
#'
#' smrmul(fit1,alternative = "linear")
#'
#' smrmul(fit1)
#'
#'
#'@import tidyverse
#'
#'@export


smrmul<-function(x,
              alternative = c("logistic","linear"),
              digit = 3,
              ...){

  if(!missing(alternative)){
    if(alternative == "logistic"){
      b<-summary(x)

      #get the p-value, coefficient, CI and odds ratio from the summary
      pvalue1<-b$coefficients[,4]
      es1<-b$coefficients[,1]

      coi1 <- b$coefficients[,1]-1.96*b$coefficients[,2]
      coi2 <- b$coefficients[,1]+1.96*b$coefficients[,2]

      thre<-exp(cbind(CondOR=b$coefficients[,1], coi1,coi2))

      pvalue1<-as.data.frame(pvalue1)
      variable1<-rownames(pvalue1)
      pvalues1<-cbind(variable1,es1,pvalue1,thre)
      rownames(pvalues1)<-NULL


      #delete the intercept row
      tal<-pvalues1[-1,]

      #choose the variables which p.value is smaller than 0.005
      del<-which(tal[,3] < 0.05)
      tal2<-tal[del,]

      #give a function to change the form of p-value

      test<-function(x){
        if(x<0.001){
          x.txt1<-"<0.001"
        }else{
          x.txt1<-"<0.05"
        }
        x.txt1
      }

      #change the form of p-value
      res1 <- numeric(nrow(tal2))
      for(i in 1:nrow(tal2)){
        res1[i]<-test(tal2[i,3])
      }

      #For loop to change the forme of the CI
      coi1 <- numeric(nrow(tal2))
      coi2 <- numeric(nrow(tal2))
      for(i in 1:nrow(tal2)){
        coi1[i]<-format(tal2[i,5],digits = digit)
        coi2[i]<-format(tal2[i,6],digits = digit)
      }

      #for loop to change the form of odds ratio
      or1<-numeric(nrow(tal2))
      for(i in 1:nrow(tal2)){
        or1[i]<-format(tal2[i,4],digits = digit)
      }

      #for loop to change the forme of coefficient
      cof1<-numeric(nrow(tal2))
      for(i in 1:nrow(tal2)){
        cof1[i]<-format(tal2[i,2],digits = digit)
      }

      #combine the p-value and the name of variavles and CI
      out1 <- data.frame(Parameter = as.character(tal2[,1]),
                         Coefficient = cof1,
                         p.value = res1,
                         OR = or1,
                         CI= paste0("(",
                                    coi1,
                                    ",",
                                    coi2,
                                    ")"))
      names(out1)[5] <- "CI(95%)"
      out1
    }else{
      a<-summary(x)
      ci<-confint(x)

      #get the p-value from the summary
      pvalue<-a$coefficients[,4]
      es<-a$coefficients[,1]
      pvalue<-as.data.frame(pvalue)
      variable<-rownames(pvalue)
      pvalues<-cbind(variable,es,ci,pvalue)
      rownames(pvalues)<-NULL

      #delete the intercept row
      ta<-pvalues[-1,]

      #choose the variables which p.value is smaller than 0.005
      de<-which(ta[,5] < 0.05)
      ta2<-ta[de,]

      #give a function to change the form of p-value

      test<-function(x){
        if(x<0.001){
          x.txt1<-"<0.001"
        }else{
          x.txt1<-"<0.05"
        }
        x.txt1
      }


      #change the form of p-value
      res <- numeric(nrow(ta2))
      for(i in 1:nrow(ta2)){
        res[i]<-test(ta2[i,5])
      }

      #For loop to change the forme of the CI
      ci1 <- numeric(nrow(ta2))
      ci2 <- numeric(nrow(ta2))
      for(i in 1:nrow(ta2)){
        ci1[i]<-format(ta2[i,3],digits = digit)
        ci2[i]<-format(ta2[i,4],digits = digit)
      }

      #for loop to change the forme of coefficient
      cof<-numeric(nrow(ta2))
      for(i in 1:nrow(ta2)){
        cof[i]<-format(ta2[i,2],digits = digit)
      }

      #combine the p-value and the name of variavles and CI
      out <- data.frame(Parameter = as.character(ta2[,1]),
                        Coefficient = cof,
                        CI= paste0("(",
                                   ci1,
                                   ",",
                                   ci2,
                                   ")"),
                        p.value=res)
      names(out)[3] <- "CI(95%)"

      out
    }
  }else{
    a<-summary(x)
    ci<-confint(x)

    #get the p-value from the summary
    pvalue<-a$coefficients[,4]
    es<-a$coefficients[,1]
    pvalue<-as.data.frame(pvalue)
    variable<-rownames(pvalue)
    pvalues<-cbind(variable,es,ci,pvalue)
    rownames(pvalues)<-NULL

    #delete the intercept row
    ta<-pvalues[-1,]

    #choose the variables which p.value is smaller than 0.005
    de<-which(ta[,5] < 0.05)
    ta2<-ta[de,]

    #give a function to change the form of p-value

    test<-function(x){
      if(x<0.001){
        x.txt1<-"<0.001"
      }else{
        x.txt1<-"<0.05"
      }
      x.txt1
    }


    #change the form of p-value
    res <- numeric(nrow(ta2))
    for(i in 1:nrow(ta2)){
      res[i]<-test(ta2[i,5])
    }

    #For loop to change the forme of the CI
    ci1 <- numeric(nrow(ta2))
    ci2 <- numeric(nrow(ta2))
    for(i in 1:nrow(ta2)){
      ci1[i]<-format(ta2[i,3],digits = digit)
      ci2[i]<-format(ta2[i,4],digits = digit)
    }

    #for loop to change the forme of coefficient
    cof<-numeric(nrow(ta2))
    for(i in 1:nrow(ta2)){
      cof[i]<-format(ta2[i,2],digits = digit)
    }

    #combine the p-value and the name of variavles and CI
    out <- data.frame(Parameter = as.character(ta2[,1]),
                      Coefficient = cof,
                      CI= paste0("(",
                                 ci1,
                                 ",",
                                 ci2,
                                 ")"),
                      p.value=res)
    names(out)[3] <- "CI(95%)"
    out
  }
}
YuweiNi45/lng documentation built on May 12, 2019, 6:26 p.m.