简体   繁体   English

我们如何从欧洲防风草对象中提取协方差矩阵?

[英]How can we extract covariance matrix from a parsnip object?

I am trying to use tidymodels ecosystem to perform econometric analysis.我正在尝试使用 tidymodels 生态系统进行计量经济学分析。 The example I am following at the moment is from the book “Principles of Econometrics with R” by Colonescu .我目前正在遵循的示例来自Colonescu 所著的 Rinciples of Econometrics with R》一 The data from the book can be downloaded through书中的数据可以通过以下方式下载

devtools::install_github("ccolonescue/PoEData")

0.1 The Example 0.1 例子

I am creating a wage discrimination model, which has interaction effects as well.我正在创建一个工资歧视模型,它也具有交互作用。 The model is as follows模型如下

library(tidymodels)
library(PoEdata)#to load the data
library(car)#For linearHypothesis function

Loading required package: carData加载所需的包:carData

lm_model <- linear_reg() %>% 
     set_engine("lm")#model specification
data("cps4_small")
mod1 <- lm_model %>% 
     fit(wage~educ+black*female, data=cps4_small)#model fitting

0.2 The Issue 0.2 问题

After creating the model, I want to test the hypothesis that there is no discrimination on the basis of gender or race.创建模型后,我想测试不存在基于性别或种族的歧视的假设。 In other words, I need to test the hypothesis that the coefficients of black, female, and black:female are all zero at the same type.换句话说,我需要检验相同类型的 black、female 和 black:female 的系数都为零的假设。 I want to use linearHypothesis function from the car package for this.我想为此使用 car 包中的linearHypothesis函数。

hyp <- c("black=0", "female=0", "black:female=0")
tab <- tidy(linearHypothesis(mod1, hyp))

This gives me an error that there is no applicable method for vcov for an object of class _lm or model_fit .这给了我一个错误,即there is no applicable method for vcov for an object of class _lm or model_fit

So, can someone help me how I can generate covariance matrix from a parsnip object?那么,有人可以帮助我如何从欧洲防风草对象生成协方差矩阵吗?

You need to use the extract_fit_engine() to get out the underlying lm fit object from the parsnip model object.您需要使用extract_fit_engine()parsnip模型对象中取出底层lm拟合对象。

library(tidymodels)
library(PoEdata)
library(car)

data("cps4_small")

lm_model <- linear_reg() %>% 
  set_engine("lm")

mod1 <- lm_model %>% 
  fit(wage ~ educ + black * female, data = cps4_small)

hyp <- c("black=0", "female=0", "black:female=0")

mod1 %>%
  extract_fit_engine() %>%
  linearHypothesis(hyp) %>%
  tidy()
#> # A tibble: 2 × 6
#>   res.df     rss    df sumsq statistic  p.value
#>    <dbl>   <dbl> <dbl> <dbl>     <dbl>    <dbl>
#> 1    998 135771.    NA   NA       NA   NA      
#> 2    995 130195.     3 5576.      14.2  4.53e-9

Created on 2021-11-13 by the reprex package (v2.0.1)reprex 包(v2.0.1) 于 2021 年 11 月 13 日创建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM