R 错误:使用与nlsw88数据拟合不同的类型指定了变量
我使用STATA nlsw88中的数据并制作模型R 错误:使用与nlsw88数据拟合不同的类型指定了变量,r,R,我使用STATA nlsw88中的数据并制作模型 h<-read.dta("nlsw88.dta") h1<-mutate(h,age = log(h$age),wage = log(h$wage)) model2<-lm(data=h1,wage~age+race+married+never_married+grade+collgrad+industry+union+occupation+hours+ttl_exp+tenure+c_city) h在h1数据框中未婚和c\u
h<-read.dta("nlsw88.dta")
h1<-mutate(h,age = log(h$age),wage = log(h$wage))
model2<-lm(data=h1,wage~age+race+married+never_married+grade+collgrad+industry+union+occupation+hours+ttl_exp+tenure+c_city)
h在h1
数据框中未婚
和c\u城市
的变量类别为整数
:
class(h1$never_married)
[1] "integer"
class(h1$c_city)
[1] "integer"
但是在nd
数据框中,这些变量的类别是因子
:
class(nd$never_married)
[1] "factor"
class(nd$c_city)
[1] "factor"
因此,nd
的代码应为:
nd <- data.frame(age=log(37), married="married", union="union",
race="white", grade=14, never_married=1, collgrad="college grad",
industry="Manufacturing", occupation="Operatives", hours=48,
ttl_exp=10, tenure=5, c_city=0)
请勿将您的数据以图片形式发布,请学习如何发布。这将使其他人更容易帮助你。非常感谢
class(nd$never_married)
[1] "factor"
class(nd$c_city)
[1] "factor"
nd <- data.frame(age=log(37), married="married", union="union",
race="white", grade=14, never_married=1, collgrad="college grad",
industry="Manufacturing", occupation="Operatives", hours=48,
ttl_exp=10, tenure=5, c_city=0)
predict(model2,nd)
1
1.902962