Search code examples
pythonrscipystatisticsprobability

Equivalent for "qt" R function in Python


I have the following code in R:

n <- 112 # Observations 
p <- 4 # Variables
alpha <- 0.05 # Alpha is alpha 
quant = qt(1-alpha/2, n-p-1) # which is 1.982383

From my research, the qt function from R is related with to the t-distribution: R - qt function syntax

My question is:

How can I get the equivalent in Python?


Solution

  • This is scipy.stats.t.ppf:

    In [26]: import scipy.stats                                                                              
    In [27]: n = 112                                                                                         
    In [28]: p = 4                                                                                           
    In [29]: alpha = 0.05                                                                                    
    In [40]: scipy.stats.t.ppf(1 - alpha / 2, n - p - 1)                                                              
    Out[40]: 1.9823833701230174
    

    Alternatively, if you'd rather get rid of manually specifying that the tail is what you're interested in, there's scipy.stats.t.isf:

    In [40]: scipy.stats.t.isf(alpha / 2, n - p - 1)                                                              
    Out[40]: 1.9823833701230174