Reputation: 11657
I'm having trouble understanding the solve
command in R, even after reading some basic examples and documentation.
Let's say I have the following formula describing the growth rate of an economy:
Y = K^α * (AL)^1−α
Let's set α = 1/3. Let's read in a dummy data-frame
data <- read.table(text = "
Economy Year y k L
A 1960 100 1 1000
A 2000 1350 27 2000
B 1960 20 2 4000
B 2000 1350 54 8000
", header = TRUE)
Now, how would I use the solve
command (or something more appropriate) to calculate the value of "A" in each of these rows? Something like this doesn't work obviously:
with(data, solve(y, k^(1/3), L^(2/3))
Upvotes: 0
Views: 84
Reputation: 1350
I used the ompr
Optimization Modelling Package. You need to rearrange the equation to make it work.
library(ROI)
library(ROI.plugin.glpk)
library(ompr)
library(ompr.roi)
library(dplyr)
f <- function(y, k, L, alpha){
MIPModel() %>%
add_variable(A) %>%
set_objective(0) %>%
add_constraint((((y/(k^alpha))^(1/((1-alpha))))/L) == A) %>%
solve_model(with_ROI(solver = "glpk")) %>%
get_solution(A)
}
apply(data[,3:5], 1, function(x) f(y=x['y'],k=x['k'],L=x['L'], alpha=0.3))
#[1] 0.71968567 0.35984284 0.17992142 0.08996071
apply(data[,3:5], 1, function(x) f(y=x['y'],k=x['k'],L=x['L'], alpha=1/3))
#[1] 1.00000000 4.77297077 0.01581139 0.84375000
This might be an overkill for this example. But is scalable.
Upvotes: 1