如何在 R 中使用 stargazer 为回归模型添加标题?
要使用stargazer向回归模型添加标题,我们可以在stargazer函数中使用title参数。
例如,如果我们有一个名为Reg_Model且输出为文本的模型,则可以使用下面提到的命令添加使用stargazer的模型的标题-
stargazer(Reg_Model,type="text",title="Regression Model between x and y")
示例1
以下代码段创建了一个示例数据框-
x<-rnorm(20) y<-rnorm(20) df<-data.frame(x,y) df输出结果
创建以下数据框-
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
要在x和y之间创建回归模型,请将以下代码添加到上述代码段中-
Model<-lm(y~x,data=df) summary(Model)输出结果
如果您将上述所有给定的片段作为单个程序执行,它会生成以下输出-
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
要使用stargazer获取模型输出,请将以下代码添加到上面的代码段中-
library(stargazer) stargazer(Model,type="text")输出结果
如果您将上述所有给定的片段作为单个程序执行,它会生成以下输出-
=============================================== 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
要获得带有标题的模型输出观星者,请将以下代码添加到上面的代码段中-
stargazer(Model,type="text",title="Regression Model between x and y")输出结果
如果您将上述所有给定的片段作为单个程序执行,它会生成以下输出-
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
示例2
以下代码段创建了一个示例数据框-
Height<-sample(135:180,20) Weight<-sample(38:80,20) dat<-data.frame(Height,Weight) dat输出结果
创建以下数据框-
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
将以下代码添加到上述代码段-
Mod<-lm(Height~Weight,data=dat) summary(Mod)输出结果
如果您将上述所有给定的片段作为单个程序执行,它会生成以下输出-
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
将以下代码添加到上述代码段-
stargazer(Mod,type="text",title="Regression Model between Height and Weight")输出结果
如果您将上述所有给定的片段作为单个程序执行,它会生成以下输出-
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