我想知道是否有一个函数可以计算用lm()估计的模型的(经济)弹性。
在自变量的变化率为1%时,因变量的百分比变化的弹性系数为B×X / Y (自变量的b=模型系数)。
下面是Rmd文件的代码,它有一个简单的线性模型和每个系数的弹性。输出应该是变量名称和弹性的表。
---
title: "Elasticity"
output: html_document
---
```{r}N <- 1000
U <- r范数(N)
x1 <- r范数(N)
x2 <- 1+ x1 +r范数(N)
Y <- 1+ x1 + x2 +u
df <- data.frame(y,x1,x2)
fit <- lm(y ~ x1 + x2,data = df)
elax1 <- as.numeric(fit$x1)*指(df$x1)/mean(df$y)
elax2 <- as.numeric(fit$x2)*指(df$x2)/mean(df$y)
变量<-c ('x1','x2')
弹性<-c (elax1,elax2)
A <- data.frame(变量,弹性)
Output the results in a table:
```{r, message=FALSE,results='asis'}需要(望星者)
天文望远镜(a,汇总= FALSE,type = 'html',rownames=FALSE)
发布于 2015-01-23 11:26:13
我想出了自己的解决方案,也许能帮到别人。请注意,我在模型中包含了一个交互。当然,改进是值得欢迎的。
---
title: "Elasticity"
output: html_document
---
Generate data and linear model:
```{r}N <- 1000
U <- r范数(N)
x1 <- r范数(N)
x2 <- 1+ x1 +r范数(N)
Y <- 1+ x1 + x2 +u
df <- data.frame(y,x1,x2)
fit <- lm(y ~ x1 * x2,data = df)
Function to calculate elasticities:
```{r,results='asis'}弹性<-函数(Linmod){
Ncoef <- nrow(data.frame(linmod$系数))
(I in 2:Ncoef){
el <- colMeans(model.matrix(linmod))i/colMeans(model.matrix(linmod))1) (linmod$coefficientsi*as.numeric)
如果是(i== 2,弹性<- el,弹性<- rbind(弹性,el))
}
行名(弹性) <-名称(coef(Linmod)-1)
名称(弹性) <-“弹性”
返回(data.frame(弹性))
}
Run the elasticites function and produce a nice table:
```{r,results='asis',message=FALSE}A <-弹性(Fit)
需要(望星者)
天文望远镜(a,汇总= FALSE,type = 'html')
https://stackoverflow.com/questions/28087726
复制相似问题