[英]How to use Predict() a linear model given the value of B1
library(wooldridge)
library(tidyverse)
library(stargazer)
setwd("C:/Users/Charlie/Desktop/R Homework")
data(wage1)
reg_wage1 <- lm(lwage ~ female + educ + (female * educ), data = wage1)
stargazer(reg_wage1, type = "text")
female_at_zero <- data.frame(female=0)
pred <- predict(reg_wage1, female_at_zero)
stargazer(pred, type = "text")
My problem is when I try to run this code it keeps asking me to put in a value for educ, but I do not want to change education, I only want to see the model's results in stargazer if female is equal to 0.我的问题是,当我尝试运行此代码时,它一直要求我输入 educ 的值,但我不想更改教育,我只想在女性等于 0 时在 stargazer 中查看模型的结果。
The issue that you're running into is that you've defined your model reg_wage1
with educ
so it won't know how to make a prediction without an educ
value.您遇到的问题是您已经使用
educ
定义了 model reg_wage1
,因此它不知道如何在没有educ
值的情况下进行预测。 lm
has defined the female
variable based on the values of the educ
variable (not to mention the interacted variable you added to the model). lm
已根据educ
变量的值定义了female
变量(更不用说您添加到模型中的交互变量)。 If you want to see the effect on lwage
with just female = 0
, you're going to have to redefine your model.如果你想看到
female = 0
对lwage
的影响,你将不得不重新定义你的 model。
Another option is to simply call female_at_zero <- data.frame(female = 0, educ = 0)
.另一种选择是简单地调用
female_at_zero <- data.frame(female = 0, educ = 0)
。 By setting educ
to 0, you're essentially looking at just the effect of female
.通过将
educ
设置为 0,您实际上只是在查看female
的效果。 But this solution is redundant because the value of pred
will simply be the value of Constant
from the output of stargazer(reg_wage1, type = "text")
.但是这个解决方案是多余的,因为
pred
的值将只是stargazer(reg_wage1, type = "text")
的 output 中Constant
的值。 This is due to the fact that a linear model ( lm
) defines its intercept ( Constant
) as the point at which all independent variables are equal to 0.这是因为线性 model (
lm
) 将其截距 ( Constant
) 定义为所有自变量都等于 0 的点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.