如何在R中提取Translog成本函数的估计系数并保存为变量
提取Translog成本函数的估计系数
你遇到的问题是因为translogCostEst()返回的是一个列表对象,而非标准的回归模型对象,直接对整个列表用coef()自然取不到系数。实际的回归估计结果存储在列表的$est元素中,这才是可以用coef()处理的模型对象。
正确提取系数的方法
直接针对Translog_fit$est调用coef()即可:
# 提取全部估计系数 translog_coefs <- coef(Translog_fit$est) translog_coefs
如果需要把系数保存为结构化数据或合并到原数据集,可以这样操作:
# 将系数转为数据框,方便查看和后续处理 coef_df <- as.data.frame(translog_coefs) colnames(coef_df) <- "Estimated_Coefficients" # 若需将系数合并到原数据集(注意系数数量与观测数不匹配时会自动循环填充,按需使用) germanFarms <- cbind(germanFarms, coef_df)
修改后的完整代码示例
# 处理输入价格与变量 data(germanFarms) germanFarms$qOutput <- germanFarms$vOutput / germanFarms$pOutput germanFarms$vLabor <- germanFarms$pLabor + germanFarms$qLabor germanFarms$cost <- germanFarms$vLabor + germanFarms$vVarInput germanFarms$time <- c(1:20) # 估计Translog成本函数 Translog_fit <- translogCostEst(cName = "cost", yName = "qOutput", pNames = c( "pLabor", "pVarInput" ), fNames = "land", shifterNames = "time", data = germanFarms, homPrice = FALSE) summary(Translog_fit$est) # 正确提取并打印系数 translog_coefs <- coef(Translog_fit$est) print(translog_coefs) # 可选:将系数转为数据框保存 coef_df <- as.data.frame(translog_coefs) colnames(coef_df) <- "Estimated_Coefficients"
内容的提问来源于stack exchange,提问作者Patrick
相关产品推荐
相关产品推荐

