trun.d: Truncated Probability Density Function of a gamlss.family...

Description Usage Arguments Value Author(s) References See Also Examples

View source: R/trun.d.R

Description

Creates a truncated probability density function version from a current GAMLSS family distribution

For continuous distributions left truncation at 3 means that the random variable can take the value 3. For discrete distributions left truncation at 3 means that the random variable can take values from 4 onwards. This is the same for right truncation. Truncation at 15 for a discrete variable means that 15 and greater values are not allowed but for continuous variable it mean values greater that 15 are not allowed (so 15 is a possible value).

Usage

1
2
trun.d(par, family = "NO", type = c("left", "right", "both"),
       varying = FALSE, ...)

Arguments

par

a vector with one (for "left" or "right" truncation) or two elements for "both". When the argument varying = TRUE then par can be a vector or a matrix with two columns respectively.

family

a gamlss.family object, which is used to define the distribution and the link functions of the various parameters. The distribution families supported by gamlss() can be found in gamlss.family. Functions such as BI() (binomial) produce a family object.

type

whether left, right or in both sides truncation is required, (left is the default).

varying

whether the truncation varies for diferent observations. This can be usefull in regression analysis. If varying = TRUE then par should be an n-length vector for type equal "left" and "right" and an n by 2 matrix for type="both"

...

for extra arguments

Value

Returns a d family function

Author(s)

Mikis Stasinopoulos mikis.stasinopoulos@gamlss.org and Bob Rigby

References

Rigby, R. A. and Stasinopoulos D. M. (2005). Generalized additive models for location, scale and shape,(with discussion), Appl. Statist., 54, part 3, pp 507-554.

Stasinopoulos D. M., Rigby R.A. and Akantziliotou C. (2003) Instructions on how to use the GAMLSS package in R. Accompanying documentation in the current GAMLSS help files, (see also http://www.gamlss.org/).

See Also

trun.p, trun.q, trun.r, gen.trun

Examples

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#------------------------------------------------------------------------------------------
# continuous distribution 
# left truncation 
test1<-trun.d(par=c(0), family="TF", type="left")
test1(1)
dTF(1)/(1-pTF(0))
if(abs(test1(1)-(dTF(1)/pTF(0)))>0.00001) stop("error in left trucation")
test1(1, log=TRUE)
log(dTF(1)/(1-pTF(0)))
if(abs(test1(1, log=TRUE)-log(dTF(1)/pTF(0)))>0.00001) 
                   stop("error in left trucation")
integrate(function(x) test1(x, mu=-2, sigma=1, nu=1),0,Inf) 
# the pdf is defined even with negative mu
integrate(function(x) test1(x, mu=0, sigma=10, nu=1),0,Inf) 
integrate(function(x) test1(x, mu=5, sigma=5, nu=10),0,Inf)
plot(function(x) test1(x, mu=-3, sigma=1, nu=1),0,10)
plot(function(x) test1(x, mu=3, sigma=5, nu=10),0,10)
#----------------------------------------------------------------------------------------
# right truncation
test2<-trun.d(par=c(10), family="BCT", type="right")
test2(1)
dBCT(1)/(pBCT(10))
#if(abs(test2(1)-(dBCT(1)/pBCT(10)))>0.00001) stop("error in right trucation")
test2(1, log=TRUE)
log(dBCT(1)/(pBCT(10)))
if(abs(test2(1, log=TRUE)-log(dBCT(1)/(pBCT(10))))>0.00001) 
                   stop("error in right trucation")
integrate(function(x) test2(x, mu=2, sigma=1, nu=1),0,10) 
integrate(function(x) test2(x, mu=2, sigma=.1, nu=1),0,10) 
integrate(function(x) test2(x, mu=2, sigma=.1, nu=10),0,10) 
plot(function(x) test2(x, mu=2, sigma=.1, nu=1),0,10)
plot(function(x) test2(x, mu=2, sigma=1, nu=1),0,10)
#-----------------------------------------------------------------------------------------
# both left and right truncation
test3<-trun.d(par=c(-3,3), family="TF", type="both")
test3(0)
dTF(0)/(pTF(3)-pTF(-3))
if(abs(test3(0)-dTF(0)/(pTF(3)-pTF(-3)))>0.00001) 
              stop("error in right trucation")
test3(0, log=TRUE)
log(dTF(0)/(pTF(3)-pTF(-3)))
if(abs(test3(0, log=TRUE)-log(dTF(0)/(pTF(3)-pTF(-3))))>0.00001) 
            stop("error in both trucation")
plot(function(x) test3(x, mu=0, sigma=1, nu=1),-3,3)
integrate(function(x) test3(x, mu=2, sigma=1, nu=1),-3,3)
#-----------------------------------------------------------------------------------------
# discrete distribution
# left 
# Poisson truncated at zero means zero is excluded
test4<-trun.d(par=c(0), family="PO", type="left")
test4(1)
dPO(1)/(1-pPO(0))
if(abs(test4(1)-dPO(1)/(1-pPO(0)))>0.00001) stop("error in left trucation")
test4(1, log=TRUE)
log(dPO(1)/(1-pPO(0)))
if(abs(test4(1, log=TRUE)-log(dPO(1)/(1-pPO(0))))>0.00001) 
               stop("error in left trucation")
sum(test4(x=1:20, mu=2)) # 
sum(test4(x=1:200, mu=80)) #
plot(function(x) test4(x, mu=20), from=1, to=51, n=50+1, type="h") # pdf 
# right
# right truncated at 10 means 10 is excluded
test5<-trun.d(par=c(10), family="NBI", type="right")
test5(2)
dNBI(2)/(pNBI(9))
if(abs(test5(1)-dNBI(1)/(pNBI(9)))>0.00001) stop("error in right trucation")
test5(1, log=TRUE)
log(dNBI(1)/(pNBI(9)))
if(abs(test5(1, log=TRUE)-log(dNBI(1)/(pNBI(9))))>0.00001) stop("error in right trucation")
sum(test5(x=0:9, mu=2,   sigma=2)) # 
sum(test5(x=0:9, mu=300, sigma=5)) # can have mu > parameter
plot(function(x) test5(x, mu=20, sigma=3), from=0, to=9, n=10, type="h") # pdf
plot(function(x) test5(x, mu=300, sigma=5), from=0, to=9, n=10, type="h") # pdf
#----------------------------------------------------------------------------------------
# both
test6<-trun.d(par=c(0,10), family="NBI", type="both")
test6(2)
dNBI(2)/(pNBI(9)-pNBI(0))
if(abs(test6(2)-dNBI(2)/(pNBI(9)-pNBI(0)))>0.00001) 
        stop("error in right trucation")
test6(1, log=TRUE)
log(dNBI(1)/(pNBI(9)-pNBI(0)))
if(abs(test6(1, log=TRUE)-log(dNBI(1)/(pNBI(9)-pNBI(0))))>0.00001) 
  stop("error in right trucation")
sum(test6(x=1:9, mu=2,   sigma=2)) # 
sum(test6(x=1:9, mu=100, sigma=5)) # can have mu > parameter
plot(function(x) test6(x, mu=20, sigma=3), from=1, to=9, n=9, type="h") # pdf
plot(function(x) test6(x, mu=300, sigma=.4), from=1, to=9, n=9, type="h") # pdf
#------------------------------------------------------------------------------------------
# now try when the trucated points varies for each observarion
# this will be appropriate for regression models only 
# continuous
#----------------------------------------------------------------------------------------
# left truncation
test7<-trun.d(par=c(0,1,2), family="TF", type="left", varying=TRUE)
test7(c(1,2,3))
dTF(c(1,2,3))/(1-pTF(c(0,1,2)))
test7(c(1,2,3), log=TRUE)
#----------------------------------------------------------------------------------------
# right truncation
test8<-trun.d(par=c(10,11,12), family="BCT", type="right", varying=TRUE)
test8(c(1,2,3))
dBCT(c(1,2,3))/(pBCT(c(10,11,12)))
test8(c(1,2,3), log=TRUE)
#----------------------------------------------------------------------------------------
# both left and right truncation
test9<-trun.d(par=cbind(c(0,1,2),c(10,11,12) ), family="TF", type="both", 
             varying=TRUE)
test9(c(1,2,3))
dTF(c(1,2,3))/ (pTF(c(10,11,12))-pTF(c(0,1,2)))
test3(c(1,2,3), log=TRUE)
#----------------------------------------------------------------------------------------
# discrete
# left
test10<-trun.d(par=c(0,1,2), family="PO", type="left", varying=TRUE)
test10(c(1,2,3))
dPO(c(1,2,3))/(1-pPO(c(0,1,2)))
# right
test11<-trun.d(par=c(10,11,12), family="NBI", type="right", varying=TRUE)
test11(c(1,2,3))
dNBI(c(1,2,3))/pNBI(c(9,10,11))
# both
test12<-trun.d(par=rbind(c(0,10), c(1,11), c(2,12)), family="NBI", type="both", varying=TRUE)
test12(c(2,3,4))
dNBI(c(2,3,4))/(pNBI(c(9,10,11))-pNBI(c(0,1,2)))

Example output

Loading required package: gamlss.dist
Loading required package: MASS
Loading required package: gamlss
Loading required package: splines
Loading required package: gamlss.data

Attaching package:gamlss.dataThe following object is masked frompackage:datasets:

    sleep

Loading required package: nlme
Loading required package: parallel
 **********   GAMLSS Version 5.2-0  ********** 
For more on GAMLSS look at https://www.gamlss.com/
Type gamlssNews() to see new features/changes/bug fixes.

[1] 0.460724
[1] 0.460724
[1] -0.7749562
[1] -0.7749562
1 with absolute error < 8.9e-10
1 with absolute error < 1.9e-05
1 with absolute error < 5e-06
[1] 0.003767161
[1] 0.003767161
[1] -5.581434
[1] -5.581434
1 with absolute error < 4.4e-05
1 with absolute error < 1.7e-07
1 with absolute error < 5.3e-07
[1] 0.3943707
[1] 0.3943707
[1] -0.9304639
[1] -0.9304639
1 with absolute error < 2.9e-05
[1] 0.5819767
[1] 0.5819767
[1] -0.5413249
[1] -0.5413249
[1] 1
[1] 1
[1] 0.1251222
[1] 0.1251222
[1] -1.385317
[1] -1.385317
[1] 1
[1] 1
[1] 0.2504892
[1] 0.2504892
[1] -0.6911921
[1] -0.6911921
[1] 1
[1] 1
[1] 0.4607240 0.3587386 0.3106923
[1] 0.4607240 0.3587386 0.3106923
[1] -0.7749562 -1.0251612 -1.1689522
[1] 0.003767161 0.008609972 0.026385923
[1] 0.003767161 0.008609972 0.026385923
[1] -5.581434 -4.754834 -3.634925
[1] 0.4607247 0.3587393 0.3106936
[1] 0.4607247 0.3587393 0.3106936
[1] -1.454670 -2.781061 -4.460660
[1] 0.5819767 0.6961056 0.7635389
[1] 0.5819767 0.6961056 0.7635389
[1] 0.25024438 0.12506106 0.06251526
[1] 0.25024438 0.12506106 0.06251526
[1] 0.2504892 0.2504892 0.2504892
[1] 0.2504892 0.2504892 0.2504892

gamlss.tr documentation built on July 13, 2020, 5:07 p.m.

Related to trun.d in gamlss.tr...