Skip to content

Instantly share code, notes, and snippets.

@trialsolution
Created November 2, 2012 12:31
Show Gist options
  • Save trialsolution/4001055 to your computer and use it in GitHub Desktop.
Save trialsolution/4001055 to your computer and use it in GitHub Desktop.
fits a linear function to a set of points on the EV-frontier
library(gdxrrw)
library(ggplot2)
# path to the gams installation
igdx("n:/soft/gams/gams23.8_64")
# data input
gdxName <- "EV"
symName <- "EV_frontier"
frontier <- rgdx.param(gdxName,symName,names=c("step","item"))
names(frontier)[3] <- "value"
# fitting a logarithmic function
X <- data.frame(t=1:10,x=frontier[frontier$item=="E",]$value,y=frontier[frontier$item=="V",]$value)
mymodel <- lm(y~x, data=X)
# check the graph of fitting
X <- data.frame(X, fitted=mymodel$fitted.values)
p <- ggplot(X, aes(x))
p+geom_line(aes(y=fitted))+geom_point(aes(y=y),color="red")
# write out to gdx
# intercept
a <- mymodel$coefficients[[1]]
# slope
b <- mymodel$coefficients[[2]]
# The symName attribute is saved to GDX as the parametername
attr(a,"symName") <- "intercept"
attr(b,"symName") <- "slope"
#4. write data to GDX file
#wgdx.scalar("fit.gdx",a)
wgdx.lst("fit.gdx",list(a,b))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment