Home > Blockchain >  Wrap interpretation of statistical test in function
Wrap interpretation of statistical test in function

Time:12-05

I want to create a function in R that interprets the result of Goldfeld-Quandt test.

library(lmtest)
interp <- function(model, order, data, fraction){
   test_result = gqtest(model, order.by, data, fraction)
   *some part of function here that gets the result and gets the interpretation*
   



}

Basically, it's just an automation of the interpretation of the Goldfeld-Quandt test.

If I do it manually, I can always interpret it. But I have to create a function and I can't think of anything to do such a function.

a sample result of a Goldfeld-Quandt test is this

    Goldfeld-Quandt test
data:  data
GQ = 0.3843, df1 = 41, df2 = 40, p-value = 0.8921
alternative hypothesis: variance increases from segment 1 to 2

I want to scan through this result, my target is the p-value. How do I do that? can I set the result to a variable say test_result and convert it to string? Then scan through it?

CodePudding user response:

How about this:

library(lmtest)

x <- rep(c(-1,1), 50)
## generate heteroskedastic and homoskedastic disturbances
err1 <- c(rnorm(50, sd=1), rnorm(50, sd=2))
err2 <- rnorm(100)
## generate a linear relationship
y1 <- 1   x   err1
y2 <- 1   x   err2
## perform Goldfeld-Quandt test

mod <- lm(y1 ~ x)


gqfun <- function(model, alpha=.05, ...){
  test_result <- gqtest(model, ...)
  print(test_result)
  cat("Interpretation:\n")
  if(test_result$p.value >= alpha){
    cat("text if no significant result.\n")
  }
  if(test_result$p.value < alpha & test_result$alternative == "variance increases from segment 1 to 2"){
    cat("text for significant result with greater than alternative hypohtesis.\n")
  }
  if(test_result$p.value < alpha & test_result$alternative == "variance changes from segment 1 to 2"){
    cat("text for significant result with two-sided alternative hypohtesis.\n")
  }
  if(test_result$p.value < alpha & test_result$alternative == "variance decreases from segment 1 to 2"){
    cat("text for significant result with less than alternative hypohtesis.\n")
  }
}
gqfun(mod)
#> 
#>  Goldfeld-Quandt test
#> 
#> data:  model
#> GQ = 4.8726, df1 = 48, df2 = 48, p-value = 9.552e-08
#> alternative hypothesis: variance increases from segment 1 to 2
#> 
#> Interpretation:
#> text for significant result with greater than alternative hypohtesis.

Created on 2022-12-04 by the reprex package (v2.0.1)

  • Related