npregGradFactor: Gradients of Factor Variables in Nonparametric Models

Description Usage Arguments Value Author(s) See Also Examples

Description

Obtain gradients of an ordered or unordered categorical (‘factor’) variable in nonparametric kernel regression models or categorical regression splines.

Usage

1
2
3
   npregGradFactor( x, varName, onlyOwnLevels = TRUE )

   crsGradFactor( x, varName, onlyOwnLevels = TRUE )

Arguments

x

an object of class npregression (returned by npreg), or an object of class crs (returned by crs).

varName

name of the categorical variable.

onlyOwnLevels

logical. If TRUE (default), the gradients with respect to a specific level are only retured for observations that are of this level. All other gradients are NA.

Value

A matrix that contain the effects of each level of the chosen factor variable (except for the first level) on the dependent variable. In case of an unordered categorical variable, the current level is compared to the first level. In case of an ordered categorical variable, the current level is compared to the previous level. Each row of the matrix corresponds to an observation, while each column corresponds to a level.

Author(s)

Arne Henningsen

See Also

npreg, crs.

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
   data( "oecdpanel" )
   library( "miscTools" )

   ##############  unordered factor  ###################
   oecdpanel$yearFactor <- factor( oecdpanel$year )
   npModel <- npreg( growth ~ yearFactor + initgdp, 
      regtype = "ll", gradients = TRUE, data = oecdpanel )
   round( gradients( npModel ), 3 )
   
   yearGrad <- npregGradFactor( npModel, "yearFactor" )
   round( yearGrad, 3 )
   colMeans( yearGrad, na.rm = TRUE )
   colMedians( yearGrad, na.rm = TRUE )
   all.equal( gradients( npModel )[ , 1 ], rowSums( yearGrad, na.rm = TRUE ) )

   yearGradAll <- npregGradFactor( npModel, "yearFactor", onlyOwnLevels = FALSE )
   round( yearGradAll, 3 )
   colMeans( yearGradAll )
   colMedians( yearGradAll )

   all.equal( yearGradAll[ !is.na( yearGrad ) ], yearGrad[ !is.na( yearGrad ) ] )
   
   ##############  ordered factor  ###################
   oecdpanel$yearOrdered <- ordered( oecdpanel$year )
   npModelOrdered <- npreg( growth ~ yearOrdered + initgdp, 
      regtype = "ll", gradients = TRUE, data = oecdpanel )
   round( gradients( npModelOrdered ), 3 )

   yearGradOrdered <- npregGradFactor( npModelOrdered, "yearOrdered" )
   round( yearGradOrdered, 3 )
   colMeans( yearGradOrdered, na.rm = TRUE )
   colMedians( yearGrad, na.rm = TRUE )
   all.equal( gradients( npModelOrdered )[ oecdpanel$year != 1965, 1 ], 
      rowSums( yearGradOrdered, na.rm = TRUE )[ oecdpanel$year != 1965 ] )

   yearGradOrderedAll <- npregGradFactor( npModelOrdered, "yearOrdered", 
      onlyOwnLevels = FALSE )
   round( yearGradOrderedAll, 3 )
   colMeans( yearGradOrderedAll )
   colMedians( yearGradOrderedAll )

   all.equal( yearGradOrderedAll[ !is.na( yearGradOrdered ) ], 
      yearGradOrdered[ !is.na( yearGradOrdered ) ] )

micEconNP documentation built on May 2, 2019, 6:30 p.m.