5

sampleSelectionパッケージの例を使用します

## Greene( 2003 ): example 22.8, page 786
    data( Mroz87 )
    Mroz87$kids  <- ( Mroz87$kids5 + Mroz87$kids618 > 0 )
    # Two-step estimation
    test1  = heckit( lfp ~ age + I( age^2 ) + faminc + kids + educ,
                     wage ~ exper + I( exper^2 ) + educ + city, Mroz87 ) 
    # ML estimation
    test2 =  selection( lfp ~ age + I( age^2 ) + faminc + kids + educ,
                        wage ~ exper + I( exper^2 ) + educ + city, Mroz87 ) 
    pr2 <- predict(test2,Mroz87)
    pr1 <- predict(test1,Mroz87)

私の問題は、予測関数が機能しないことです。このエラーが発生します:

    Error in UseMethod("predict") : 
  no applicable method for 'predict' applied to an object of class "c('selection', 'maxLik', 'maxim', 'list')"

予測関数は多くのモデルで機能するので、なぜヘックマン回帰モデルでエラーが発生するのでしょうか。

----------- UPDATE -----------私はある程度の進歩を遂げましたが、それでもあなたの助けが必要です。比較のためにオリジナルのヘックマンモデルを作成します。

data( Mroz87 )
Mroz87$kids  <- ( Mroz87$kids5 + Mroz87$kids618 > 0 )
test1  = heckit( lfp ~ age + I( age^2 ) + faminc + kids + educ,
                 wage ~ exper + I( exper^2 ) + educ + city, Mroz87[1:600,] ) 

その後、私は自分でそれを構築し始めます。Heckmanモデルには、選択方程式が必要です。

zi* = wi γ + ui
where zi =1 if zi* >0  and zi = 0 if zi* <=0
after you calculate yi = xi*beta +ei ONLY for the cases where zi*>0

最初にプロビットモデルを作成します。

library(MASS)
#probit1 = probit(lfp ~ age + I( age^2 ) + faminc + kids + educ, Mroz87, x = TRUE, print.level = print.level -      1, iterlim = 30)
myprobit <- glm(lfp ~ age + I( age^2 ) + faminc + kids + educ, family = binomial(link = "probit"), 
                data = Mroz87[1:600,])
summary(myprobit)

モデルは、heckitコマンドの場合とまったく同じです。

次に、lmモデルを作成します。

#get predictions for the variables (the data is not needed but I specify it anyway)
selectvar <- predict(myprobit,data = Mroz87[1:600,])
#bind the prediction to the table (I build a new one in my case)
newdata = cbind(Mroz87[1:600,],selectvar)
#Build an lm model for the subset where zi>0
lm1 = lm(wage ~ exper + I( exper^2 ) + educ + city , newdata, subset = selectvar > 0)
summary(lm1)

私の今の問題は、lmモデルがheckitによって作成されたものではないということです。理由はわかりません。何か案は?

4

1 に答える 1

4

実装

これが関数の実装ですpredict.selection-それは4つの異なるタイプの予測を生成します(これはここで説明されています):

library(Formula)
library(sampleSelection)
predict.selection = function(objSelection, dfPred, 
                                 type = c('link', 'prob', 'cond', 'uncond')) {

  # construct the Formula object
  tempS = evalq(objSelection$call$selection)
  tempO = evalq(objSelection$call$outcome)

  FormHeck = as.Formula(paste0(tempO[2], '|', tempS[2], '~', tempO[3], '|', tempS[3]))

  # regressor matrix for the selection equation
  mXSelection = model.matrix(FormHeck, data = dfPred, rhs = 2)

  # regressor matrix for the outcome equation
  mXOutcome = model.matrix(FormHeck, data = dfPred, rhs = 1)

  # indices of the various parameters in selectionObject$estimate
  vIndexBetaS = objSelection$param$index$betaS
  vIndexBetaO = objSelection$param$index$betaO
  vIndexErr = objSelection$param$index$errTerms

  # get the estimates
  vBetaS = objSelection$estimate[vIndexBetaS]
  vBetaO = objSelection$estimate[vIndexBetaO]

  dLambda = objSelection$estimate[vIndexErr['rho']]*
    objSelection$estimate[vIndexErr['sigma']]

  # depending on the type of prediction requested, return
  # TODO allow the return of multiple prediction types
  pred = switch(type, 
         link = mXSelection %*% vBetaS,
         prob = pnorm(mXSelection %*% vBetaS),
         uncond = mXOutcome %*% vBetaO,
         cond = mXOutcome %*% vBetaO + 
           dnorm(temp <- mXSelection %*% vBetaS)/pnorm(temp) * dLambda)
  return(pred)
}

テスト

MLEを使用して次のHeckmanサンプル選択モデルを推定するとします。

data(Mroz87)

# define a new variable
Mroz87$kids  = (Mroz87$kids5 + Mroz87$kids618 > 0)

# create the estimation sample
Mroz87Est = Mroz87[1:600, ]

# create the hold out sample
Mroz87Holdout = Mroz87[601:nrow(Mroz87), ]

# estimate the model using MLE
heckML =  selection(selection = lfp ~ age + I(age^2) + faminc + kids + educ,
                    outcome = wage ~ exper + I(exper^2) + educ + city, data = Mroz87Est) 
summary(heckML)  

さまざまなタイプの予測は、次のように計算されます。

vProb = predict(objSelection = heckML, dfPred = Mroz87Holdout, type = 'prob')
vLink = predict(objSelection = heckML, dfPred = Mroz87Holdout, type = 'link')
vCond = predict(objSelection = heckML, dfPred = Mroz87Holdout, type = 'cond')
vUncond = predict(objSelection = heckML, dfPred = Mroz87Holdout, type = 'uncond')

これらの計算は、 Stataなどのこれらの出力を生成するプラットフォームで検証できます。

于 2014-04-09T06:57:07.717 に答える