How to add title to regression model using stargazer in R?


To add title to regression model using stargazer, we can use title argument inside stargazer function.

For example, if we have a model called Reg_Model with Output as text then the title to this model using stargazer can be added by using the below mentioned command −

stargazer(Reg_Model,type="text",title="Regression Model between x and y")

Example 1

Following snippet creates a sample data frame −

x<-rnorm(20)
y<-rnorm(20)
df<-data.frame(x,y)
df

Output

The following dataframe is created −

      x            y
1   0.80296200    1.1413965
2   0.05853869   -1.1227868
3   1.79348142   -0.7212954
4   0.64830308    0.2956645
5   0.28551170   -1.0645189
6   0.50265553    0.9082304
7   0.25883301    0.6513258
8  -0.28277606    0.5892909
9  -1.96142707    0.8310168
10  1.29804865   -0.7780162
11 -0.53807406    0.7256280
12 -0.01142374    0.3550352
13  0.61684358    0.5681672
14 -0.03707776    0.7279025
15  0.14411337    0.7942300
16  0.95380409    0.2789388
17  0.32599974    1.2477048
18 -0.80785235    0.3246518
19 -0.77913184   -0.5227336
20  0.11869989    0.4344650

To create regression model between x and y, add the following code to the above snippet −

Model<-lm(y~x,data=df)
summary(Model)

Output

If you execute all the above given snippets as a single program, it generates the following Output −

Call:
lm(formula = y ~ x, data = df)

Residuals:
   Min     1Q     Median  3Q    Max
-1.4303 -0.2917 0.1538  0.3906 0.9988

Coefficients:
   Estimate Std. Error t value Pr(>|t|)
(Intercept)  0.3204  0.1645  1.947  0.0673 .
     x      -0.2191  0.2019  -1.085 0.2921
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.7196 on 18 degrees of freedom
Multiple R-squared: 0.06142, Adjusted R-squared: 0.009276
F-statistic: 1.178 on 1 and 18 DF, p-value: 0.2921

To get the model Output by using stargazer, add the following code to the above snippet −

library(stargazer)
stargazer(Model,type="text")

Output

If you execute all the above given snippets as a single program, it generates the following Output −

===============================================
      Dependent variable:
      ---------------------------
         y
-----------------------------------------------
      x -0.219
      (0.202)

Constant 0.320*
      (0.165)

-----------------------------------------------
Observations            20
R2                     0.061
Adjusted R2            0.009
Residual Std. Error  0.720 (df = 18)
F Statistic         1.178 (df = 1; 18)
===============================================
Note: *p<0.1; **p<0.05; ***p<0.01

To get the model Output stargazer with title, add the following code to the above snippet −

stargazer(Model,type="text",title="Regression Model between x and y")

Output

If you execute all the above given snippets as a single program, it generates the following Output −

Regression Model between x and y
===============================================
      Dependent variable:
   ---------------------------
            y
-----------------------------------------------
         x -0.219
         (0.202)

Constant 0.320*
         (0.165)

-----------------------------------------------
Observations             20
R2                     0.061
Adjusted R2             0.009
Residual Std. Error  0.720 (df = 18)
F Statistic 1.178 (df = 1; 18)
===============================================
Note: *p<0.1; **p<0.05; ***p<0.01

Example 2

Following snippet creates a sample data frame −

Height<-sample(135:180,20)
Weight<-sample(38:80,20)
dat<-data.frame(Height,Weight)
dat

Output

The following dataframe is created −

  Height Weight
1  172    56
2  149    49
3  163    76
4  135    73
5  138    75
6  168    54
7  169    45
8  165    63
9  178    79
10 159    55
11 150    47
12 171    65
13 147    53
14 173    39
15 162    57
16 144    46
17 136    40
18 156    43
19 142    42
20 151    78

Add the following code to the above snippet −

Mod<-lm(Height~Weight,data=dat)
summary(Mod)

Output

If you execute all the above given snippets as a single program, it generates the following Output −

Call:
lm(formula = Height ~ Weight, data = dat)

Residuals:
   Min    1Q    Median 3Q    Max
-23.007 -9.606 1.867  12.345 19.399

Coefficients:
             Estimate Std. Error    t value Pr(>|t|)
(Intercept) 150.78696 13.61725    11.073   1.82e-09 ***
Weight     0.09891    0.23376    0.423     0.677
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 13.75 on 18 degrees of freedom
Multiple R-squared: 0.009848, Adjusted R-squared: -0.04516
F-statistic: 0.179 on 1 and 18 DF, p-value: 0.6772

Add the following code to the above snippet −

stargazer(Mod,type="text",title="Regression Model between Height and Weight")

Output

If you execute all the above given snippets as a single program, it generates the following Output −

Regression Model between Height and Weight
===============================================
         Dependent variable:
---------------------------
            Height
-----------------------------------------------
Weight       0.099
            (0.234)

Constant    150.787***
            (13.617)

-----------------------------------------------
Observations          20
R2                  0.010
Adjusted R2        -0.045
Residual Std. Error 13.746 (df = 18)
F Statistic 0.179 (df = 1; 18)
===============================================
Note: *p<0.1; **p<0.05; ***p<0.01

Updated on: 02-Nov-2021

959 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements