I am trying to replicate the t-test with mathematical formulas and it somehow wont work with may current attempt below:
# PDF t-distribution:
t_distr = function(x,df){
t_1 = gamma((df+1)/2)/(sqrt(df*pi)*gamma(df/2))
t_2 = (1 + (seq^2/df))^(-(df+1)/2)
t_distr = t_1*t_2
return(t_distr)
} # End of function
# Initialize Sequence:
seq = seq(-4,4,by = .1)
# Integrating up to t-value of 1 (lower tail t-test):
integrate(t_distr, df = length(seq)-1, lower = -Inf, upper = 1 )
# For df = length(seq)-2 (e.g. for a lin. reg):
integrate(t_distr, df = length(seq)-2, lower = -Inf, upper = 1 )
The above code will result in the following error message:
Error in integrate(t_distr, df = length(seq) - 1, lower = -Inf, upper = 1) :
evaluation of function gave a result of wrong length
Any suggestions how to make it possible to integrate the pdf of a t-distribution using the integrate function? It also does not work with a "df" of just n...