# Calculation of elasticity for non-linear curve

i've wrote this algorithm for the calculation of the elasticity for econometric analisys, basically, this algorithm, estimate this function:

$\epsilon(x) = \frac{x}{f(x)} f'(x)$

###############################################       elasticity<-function(y.p,x.p,band){                               require(KernSmooth)                                   as.vector(y.p);as.vector(x.p)             loess.fun<-locpoly(x.p,y.p,bandwidth=band)                 plot(loess.fun,type='l',lwd=2,lty=4,                     ylim=c(0,100),xlab="Price",                     ylab="Share", col=2,                     main="Local Polynomials Estimation")             loess.der<-locpoly(x.p,y.p,drv=1,bandwidth=band,                           gridsize = length(y.p))             y1<-as.vector(loess.der\$y)             eps<-matrix(NA,length(y.p),2)             eps[,1]<-x.p                 for(i in 1:length(y.p)){                     eps[i,2]<-x.p[i]/y.p[i]*y1[i]                 }             j<-order(x.p)             colnames(eps)<-c("Price","Elasticity")         return(eps[j,])     }  ###############################################  


can i improve this algorithm? i hope that this algorithm can be useful for someone! Thanks guys!

Replay

Category: r Time: 2016-07-29 Views: 0
Tags: