is there any good way to make this function more neat and clean and or in one line

Test <- function(xbar, mu0, s, n, alpha)
# xbar = sample mean
# mu0 = mean of population
# s = standard deviation of sample
# n = sample size
# alpha = alpha risk
{
  t <- round(abs(qt(alpha/2, n-1)), digits = 3)
  T <- (xbar-mu0)/s*sqrt(n)
  print("critical value t(alpha) =")
  print(t)
  print("Teststatistic T =")
  print(T)
  if (abs(T) > t)
  {
    print("H0 rejected")
  } else
  {
    print("H0 accepted")
  }
}
> Test(57.1, 55, 1.4, 36, 0.01)

[1] "critical value t(alpha) ="

[1] 2.724

[1] "Teststatistic T ="

[1] 9

[1] "H0 rejected"

Three questions/comments

  1. What are you trying to do that
student <- t.test(1:10, y = c(7:20))
student
#> 
#>  Welch Two Sample t-test
#> 
#> data:  1:10 and c(7:20)
#> t = -5.4349, df = 21.982, p-value = 1.855e-05
#> alternative hypothesis: true difference in means is not equal to 0
#> 95 percent confidence interval:
#>  -11.052802  -4.947198
#> sample estimates:
#> mean of x mean of y 
#>       5.5      13.5
str(student)
#> List of 10
#>  $ statistic  : Named num -5.43
#>   ..- attr(*, "names")= chr "t"
#>  $ parameter  : Named num 22
#>   ..- attr(*, "names")= chr "df"
#>  $ p.value    : num 1.86e-05
#>  $ conf.int   : num [1:2] -11.05 -4.95
#>   ..- attr(*, "conf.level")= num 0.95
#>  $ estimate   : Named num [1:2] 5.5 13.5
#>   ..- attr(*, "names")= chr [1:2] "mean of x" "mean of y"
#>  $ null.value : Named num 0
#>   ..- attr(*, "names")= chr "difference in means"
#>  $ stderr     : num 1.47
#>  $ alternative: chr "two.sided"
#>  $ method     : chr "Welch Two Sample t-test"
#>  $ data.name  : chr "1:10 and c(7:20)"
#>  - attr(*, "class")= chr "htest"

Created on 2019-12-24 by the reprex package (v0.3.0)

doesn't get you?

  1. It may just be my installation, but I had to use stats::qt

  2. Functions should be, above all else, easily readable and understandable. Sometimes, conciseness aids that goal and sometimes it gets in the way.

1 Like

If you use cat instead of print you can avoid the [1] that prints on each line, and it also allows you to print multiple things on a line without having to concatenate them yourself. You have to add end of line "\n" yourself.

I would be inclined to write your function like this:

Test <- function(xbar, mu0, s, n, alpha)
  # xbar = sample mean
  # mu0 = mean of population
  # s = standard deviation of sample
  # n = sample size
  # alpha = alpha risk
{
  t <- round(abs(qt(alpha/2, n-1)), digits = 3)
  Tstat <- (xbar-mu0)/s*sqrt(n)

  cat(
    sprintf(
      paste0(c("Critical values of t(alpha) = %s\n", 
               "Test statistics T = %s\n", 
               "%s\n\n", 
               collapse = "\n")),
      t,  # first %s
      Tstat,  # second %s
      if (abs(Tstat) > t) "H0 rejected" else "H0 not rejected" # third %s
    ), 
    sep = ""
  )
}

Test(57.1, 55, 1.4, 36, 0.01)

some notes:

  1. In sprintf, you are building a string. The %s is a placeholder for a string to be added later. The values to be inserted are given as subsequent arguments. I often find this easier to read and understand than a lot of pasteing. (You may also want to see the glue package for similar functionality).
  2. I changed your T variable to Tstat. T is, by default, a synonym for TRUE. You can overwrite it, but you might confuse yourself or others sometime in the future. I recommend avoiding it as an object name.
  3. I changed your phrasing of H0 accepted to H0 not rejected. Hypothesis tests don't typically confirm a hypothesis. We either have enough evidence on hand to reject it, or we don't.
3 Likes

I strongly recommend the glue package, or the stringr wrappers, for building strings. Here's my adaptation of the prior response using the tidyverse:

library(tidyverse)

Test <- function(xbar, mu0, s, n, alpha)
  # xbar = sample mean
  # mu0 = mean of population
  # s = standard deviation of sample
  # n = sample size
  # alpha = alpha risk
{
  t <- round(abs(stats::qt(alpha/2, n-1)), digits = 3)
  Tstat <- (xbar-mu0)/s*sqrt(n)

  "Critical values of t(alpha) = {t}
  Test statistics T = {Tstat}
  {if (abs(Tstat) > t) 'H0 rejected' else 'H0 not rejected'}" %>%
    str_glue() %>% 
    cat()
}

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.