library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(data.table)

Attaching package: 'data.table'

The following objects are masked from 'package:lubridate':

    hour, isoweek, mday, minute, month, quarter, second, wday, week,
    yday, year

The following objects are masked from 'package:dplyr':

    between, first, last

The following object is masked from 'package:purrr':

    transpose
library(sandwich)
library(jtools)
library(lmtest) 
Loading required package: zoo

Attaching package: 'zoo'

The following objects are masked from 'package:data.table':

    yearmon, yearqtr

The following objects are masked from 'package:base':

    as.Date, as.Date.numeric
library(estimatr)
library(wooldridge)
library(AER)
Loading required package: car
Loading required package: carData

Attaching package: 'car'

The following object is masked from 'package:dplyr':

    recode

The following object is masked from 'package:purrr':

    some

Loading required package: survival
library(car)
library(openintro)
Loading required package: airports
Loading required package: cherryblossom
Loading required package: usdata

Attaching package: 'openintro'

The following object is masked from 'package:survival':

    transplant

The following object is masked from 'package:car':

    densityPlot

The following object is masked from 'package:wooldridge':

    prison

The following object is masked from 'package:jtools':

    movies
library(ggpubr)
library(wooldridge)
model_wage <- wage1 |> 
  lm(wage ~ educ + tenure + female + married, data = _)
model_robust <- wage1 |> 
  lm_robust(wage ~ educ + tenure + female + married, data = _)
export_summs(model_wage, model_robust, coeftest(model_wage, vcov = vcovHC), coeftest(model_wage,vcov = vcovCL))
Original model not retained as part of coeftest object. For additional model summary information (r.squared, df, etc.), consider passing `glance.coeftest()` an object where the underlying model has been saved, i.e.`lmtest::coeftest(..., save = TRUE)`.
This message is displayed once per session.
Model 1Model 2Model 3Model 4
(Intercept)-1.14    -1.14    -1.14    -1.14    
(0.66)   (0.75)   (0.76)   (0.74)   
educ0.53 ***0.53 ***0.53 ***0.53 ***
(0.05)   (0.06)   (0.06)   (0.06)   
tenure0.15 ***0.15 ***0.15 ***0.15 ***
(0.02)   (0.03)   (0.03)   (0.03)   
female-1.71 ***-1.71 ***-1.71 ***-1.71 ***
(0.27)   (0.25)   (0.25)   (0.25)   
married0.69 *  0.69 ** 0.69 ** 0.69 ** 
(0.27)   (0.25)   (0.25)   (0.25)   
N526       526       526       526       
R20.37    0.37                  
*** p < 0.001; ** p < 0.01; * p < 0.05.
bptest(model_wage)

    studentized Breusch-Pagan test

data:  model_wage
BP = 43.443, df = 4, p-value = 8.373e-09
linearHypothesis(model_wage, "educ = 0", type = c("F"))

Linear hypothesis test:
educ = 0

Model 1: restricted model
Model 2: wage ~ educ + tenure + female + married

  Res.Df    RSS Df Sum of Sq      F    Pr(>F)    
1    522 5652.7                                  
2    521 4545.2  1    1107.6 126.96 < 2.2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
linearHypothesis(model_wage, c("educ = 0", "female = 0"), type = c("F"))

Linear hypothesis test:
educ = 0
female = 0

Model 1: restricted model
Model 2: wage ~ educ + tenure + female + married

  Res.Df    RSS Df Sum of Sq      F    Pr(>F)    
1    523 6137.7                                  
2    521 4545.2  2    1592.6 91.275 < 2.2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
linearHypothesis(model_wage, c("educ = 0", "female = 0", "tenure = 0", "married = 0"), type = c("F"))

Linear hypothesis test:
educ = 0
female = 0
tenure = 0
married = 0

Model 1: restricted model
Model 2: wage ~ educ + tenure + female + married

  Res.Df    RSS Df Sum of Sq      F    Pr(>F)    
1    525 7160.4                                  
2    521 4545.2  4    2615.3 74.945 < 2.2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
lm(mpg ~ wt, data = mtcars) |> 
  summary() 

Call:
lm(formula = mpg ~ wt, data = mtcars)

Residuals:
    Min      1Q  Median      3Q     Max 
-4.5432 -2.3647 -0.1252  1.4096  6.8727 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  37.2851     1.8776  19.858  < 2e-16 ***
wt           -5.3445     0.5591  -9.559 1.29e-10 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 3.046 on 30 degrees of freedom
Multiple R-squared:  0.7528,    Adjusted R-squared:  0.7446 
F-statistic: 91.38 on 1 and 30 DF,  p-value: 1.294e-10
t <- model_wage$coef[1]/model_wage$se[1]
model_wage$coef
(Intercept)        educ      tenure      female     married 
 -1.1385271   0.5293624   0.1541769  -1.7104993   0.6852168 
#std.error of the first coefficient
summary(model_wage)

Call:
lm(formula = wage ~ educ + tenure + female + married, data = wage1)

Residuals:
    Min      1Q  Median      3Q     Max 
-7.5538 -1.7647 -0.5327  1.0318 13.9883 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) -1.13853    0.65517  -1.738   0.0828 .  
educ         0.52936    0.04698  11.268  < 2e-16 ***
tenure       0.15418    0.01871   8.241 1.40e-15 ***
female      -1.71050    0.26611  -6.428 2.93e-10 ***
married      0.68522    0.27466   2.495   0.0129 *  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 2.954 on 521 degrees of freedom
Multiple R-squared:  0.3652,    Adjusted R-squared:  0.3604 
F-statistic: 74.95 on 4 and 521 DF,  p-value: < 2.2e-16
summary(model_wage)[[4]][1,2]
[1] 0.6551687
model_wage$terms
wage ~ educ + tenure + female + married
attr(,"variables")
list(wage, educ, tenure, female, married)
attr(,"factors")
        educ tenure female married
wage       0      0      0       0
educ       1      0      0       0
tenure     0      1      0       0
female     0      0      1       0
married    0      0      0       1
attr(,"term.labels")
[1] "educ"    "tenure"  "female"  "married"
attr(,"order")
[1] 1 1 1 1
attr(,"intercept")
[1] 1
attr(,"response")
[1] 1
attr(,".Environment")
<environment: R_GlobalEnv>
attr(,"predvars")
list(wage, educ, tenure, female, married)
attr(,"dataClasses")
     wage      educ    tenure    female   married 
"numeric" "numeric" "numeric" "numeric" "numeric" 
normTail(M = c(-2.77, 2.77), df = 24)

normTail(M = c(-2.15, 2.15), df = 24)

normTail(M = c(-0.83, 0.83), df = 24)

Puromycin |> 
  ggplot(aes(x = conc, y = rate)) +
  geom_point() +
  geom_smooth(formula = y ~ log(x)) + 
  stat_regline_equation(formula = y ~ log(x))
`geom_smooth()` using method = 'loess'

mtcars |> 
  mutate(cyl = factor(cyl)) |>
  lm(mpg ~ wt + cyl, data = _) |>
  summary()

Call:
lm(formula = mpg ~ wt + cyl, data = mutate(mtcars, cyl = factor(cyl)))

Residuals:
    Min      1Q  Median      3Q     Max 
-4.5890 -1.2357 -0.5159  1.3845  5.7915 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  33.9908     1.8878  18.006  < 2e-16 ***
wt           -3.2056     0.7539  -4.252 0.000213 ***
cyl6         -4.2556     1.3861  -3.070 0.004718 ** 
cyl8         -6.0709     1.6523  -3.674 0.000999 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 2.557 on 28 degrees of freedom
Multiple R-squared:  0.8374,    Adjusted R-squared:   0.82 
F-statistic: 48.08 on 3 and 28 DF,  p-value: 3.594e-11
mtcars |> 
  mutate(cyl = factor(cyl, levels = c(6, 4, 8))) |>
  lm(mpg ~ wt + cyl, data = _) |>
  summary()

Call:
lm(formula = mpg ~ wt + cyl, data = mutate(mtcars, cyl = factor(cyl, 
    levels = c(6, 4, 8))))

Residuals:
    Min      1Q  Median      3Q     Max 
-4.5890 -1.2357 -0.5159  1.3845  5.7915 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  29.7352     2.5410  11.702 2.69e-12 ***
wt           -3.2056     0.7539  -4.252 0.000213 ***
cyl4          4.2556     1.3861   3.070 0.004718 ** 
cyl8         -1.8153     1.3576  -1.337 0.191956    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 2.557 on 28 degrees of freedom
Multiple R-squared:  0.8374,    Adjusted R-squared:   0.82 
F-statistic: 48.08 on 3 and 28 DF,  p-value: 3.594e-11
回到顶部