R Under development (unstable) (2025-10-03 r88899 ucrt) -- "Unsuffered Consequences" Copyright (C) 2025 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > library( micEcon ) If you have questions, suggestions, or comments regarding one of the 'micEcon' packages, please use a forum or 'tracker' at micEcon's R-Forge site: https://r-forge.r-project.org/projects/micecon/ > options( digits = 3 ) > > data( germanFarms ) > # output quantity: > germanFarms$qOutput <- germanFarms$vOutput / germanFarms$pOutput > # quantity of variable inputs > germanFarms$qVarInput <- germanFarms$vVarInput / germanFarms$pVarInput > # a time trend to account for technical progress: > germanFarms$time <- c(1:20) > > # estimate a Cobb-Douglas production function > estResult <- translogEst( "qOutput", c( "qLabor", "qVarInput", "land", "time" ), + germanFarms, linear = TRUE ) > > # calculate fitted values > fitted <- cobbDouglasCalc( c( "qLabor", "qVarInput", "land", "time" ), + data = germanFarms, coef = coef( estResult )[ 1:5 ], + coefCov = vcov( estResult )[ 1:5, 1:5 ] ) > print( fitted ) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 916 1024 1079 1140 1161 1195 1242 1253 1232 1289 1343 1537 1620 1668 1617 1625 17 18 19 20 1691 1760 1875 1989 attr(,"variance") [1] 329.2 154.1 109.7 111.8 83.1 128.9 333.5 125.1 214.0 267.9 [11] 155.1 478.4 594.8 484.3 218.3 157.2 379.7 498.1 594.5 1227.5 > # t-values > print( c( fitted ) / attributes( fitted )$variance^0.5 ) 1 2 3 4 5 6 7 8 9 10 11 12 13 50.5 82.5 103.0 107.8 127.3 105.3 68.0 112.1 84.2 78.7 107.8 70.3 66.4 14 15 16 17 18 19 20 75.8 109.4 129.6 86.8 78.9 76.9 56.8 > all.equal( fitted, estResult$fitted, check.attributes = FALSE ) [1] TRUE > > # calculate logged variables > germanFarms$lQLabor <- log( germanFarms$qLabor ) > germanFarms$lLand <- log( germanFarms$land ) > germanFarms$lQVarInput <- log( germanFarms$qVarInput ) > germanFarms$lTime <- log( germanFarms$time ) > germanFarms$lQOutput <- log( germanFarms$qOutput ) > > # estimation with logged variables > estResultLog <- translogEst( "lQOutput", + c( "lQLabor", "lQVarInput", "lLand", "lTime" ), + germanFarms, dataLogged = TRUE, linear = TRUE ) > all.equal( estResult[c(2:5,7:11,14:15)], estResultLog[c(2:5,7:11,14:15)] ) [1] TRUE > > # calculate fitted values using logged independent variables > fittedLogged <- cobbDouglasCalc( c( "lQLabor", "lQVarInput", "lLand", "lTime" ), + data = germanFarms, coef = coef( estResult )[ 1:5 ], + coefCov = vcov( estResult )[ 1:5, 1:5 ], dataLogged = TRUE ) > all.equal( fitted, exp( fittedLogged ), check.attributes = FALSE ) [1] TRUE > all.equal( attributes( fitted )$variance/fitted^2, + attributes( fittedLogged )$variance, check.attributes = FALSE ) [1] TRUE > all.equal( fittedLogged, predict( estResult$est, se.fit=T )$fit, + check.attributes = FALSE ) [1] TRUE > all.equal( attributes( fittedLogged )$variance^0.5, + predict( estResult$est, se.fit=T )$se.fit ) [1] TRUE > > # coefficients not named > coefNoNames <- coef( estResult )[ 1:5 ] > names( coefNoNames ) <- NULL > fittedNoNames <- cobbDouglasCalc( c( "qLabor", "qVarInput", "land", "time" ), + data = germanFarms, coef = coefNoNames ) > all.equal( fitted, fittedNoNames, check.attributes = FALSE ) [1] TRUE > > # coefficients in a different order > coefDiffOrder <- coef( estResult )[ c( 3, 5, 1, 2, 4 ) ] > fittedDiffOrder <- cobbDouglasCalc( c( "qLabor", "qVarInput", "land", "time" ), + data = germanFarms, coef = coefDiffOrder ) > all.equal( fitted, fittedDiffOrder, check.attributes = FALSE ) [1] TRUE > > ## derivatives (marginal products) > # compute the marginal products of the inputs (with "fitted" Output) > margProducts <- cobbDouglasDeriv( c( "qLabor", "qVarInput", "land", "time" ), + data = germanFarms, coef = coef( estResult )[1:5], + coefCov = vcov( estResult )[1:5, 1:5] ) > print( margProducts ) $deriv qLabor qVarInput land time 1 99.1 0.397 30.4 84.04 2 110.0 0.407 32.8 46.98 3 116.7 0.428 33.9 33.00 4 120.0 0.421 35.5 26.15 5 122.2 0.432 36.1 21.30 6 126.7 0.439 36.7 18.28 7 133.5 0.445 37.2 16.28 8 131.0 0.467 37.4 14.37 9 129.7 0.464 37.9 12.56 10 131.2 0.479 38.3 11.82 11 136.8 0.475 38.9 11.21 12 141.7 0.487 39.9 11.75 13 148.4 0.488 40.4 11.43 14 155.6 0.504 40.2 10.93 15 152.7 0.529 39.8 9.89 16 157.2 0.545 39.5 9.32 17 162.6 0.582 39.1 9.13 18 169.2 0.605 38.9 8.97 19 180.3 0.619 39.0 9.05 20 191.3 0.623 39.2 9.13 $variance qLabor qVarInput land time 1 27588 0.0719 9.77 162.44 2 33680 0.0775 10.73 52.18 3 37814 0.0854 11.27 27.43 4 39920 0.0837 11.51 17.05 5 41467 0.0877 11.49 11.89 6 44358 0.0909 12.06 8.87 7 48837 0.0950 13.24 6.99 8 47753 0.1008 12.12 5.80 9 46814 0.0994 11.85 4.62 10 48283 0.1045 11.67 4.11 11 52060 0.1050 12.47 3.54 12 56541 0.1105 12.69 3.56 13 61626 0.1129 13.79 3.20 14 67323 0.1216 14.79 2.91 15 65346 0.1295 13.85 2.61 16 68996 0.1375 14.47 2.37 17 74058 0.1541 14.97 2.31 18 80087 0.1665 15.73 2.21 19 90148 0.1775 17.15 2.13 20 100577 0.1847 18.80 2.04 attr(,"class") [1] "cobbDouglasDeriv" > # t-values > margProducts$deriv / margProducts$variance^0.5 qLabor qVarInput land time 1 0.597 1.48 9.71 6.59 2 0.599 1.46 10.02 6.50 3 0.600 1.46 10.10 6.30 4 0.601 1.45 10.46 6.33 5 0.600 1.46 10.66 6.18 6 0.601 1.46 10.58 6.14 7 0.604 1.44 10.22 6.16 8 0.600 1.47 10.75 5.97 9 0.599 1.47 11.02 5.84 10 0.597 1.48 11.22 5.83 11 0.599 1.47 11.02 5.95 12 0.596 1.47 11.20 6.23 13 0.598 1.45 10.87 6.39 14 0.600 1.45 10.45 6.40 15 0.597 1.47 10.69 6.12 16 0.599 1.47 10.39 6.06 17 0.597 1.48 10.10 6.01 18 0.598 1.48 9.81 6.03 19 0.600 1.47 9.41 6.20 20 0.603 1.45 9.03 6.38 > > # compute the marginal products of the inputs (with observed Output) > margProductsObs <- cobbDouglasDeriv( c( "qLabor", "qVarInput", "land", "time" ), + data = germanFarms, coef = coef( estResult )[1:5], + coefCov = vcov( estResult )[1:5, 1:5], yName = "qOutput" ) > print( margProductsObs ) $deriv qLabor qVarInput land time 1 102 0.410 31.4 86.81 2 103 0.383 30.9 44.17 3 116 0.425 33.6 32.71 4 123 0.432 36.5 26.88 5 124 0.439 36.7 21.62 6 126 0.435 36.4 18.13 7 133 0.444 37.1 16.25 8 135 0.480 38.5 14.78 9 127 0.455 37.2 12.31 10 130 0.476 38.0 11.73 11 139 0.481 39.4 11.35 12 142 0.489 40.0 11.78 13 144 0.474 39.3 11.12 14 158 0.513 40.9 11.12 15 155 0.538 40.5 10.06 16 157 0.546 39.6 9.32 17 159 0.570 38.3 8.93 18 169 0.604 38.8 8.95 19 182 0.623 39.2 9.11 20 192 0.624 39.2 9.14 $variance qLabor qVarInput land time 1 29169 0.0783 9.05 196.76 2 29787 0.0683 8.76 50.93 3 37278 0.0839 10.39 27.94 4 42357 0.0870 12.25 18.86 5 42815 0.0897 12.39 12.20 6 43969 0.0883 12.23 8.59 7 49363 0.0918 12.67 6.89 8 50534 0.1072 13.63 5.70 9 44993 0.0965 12.74 3.96 10 47174 0.1054 13.32 3.59 11 53434 0.1078 14.30 3.36 12 56164 0.1111 14.73 3.62 13 58000 0.1048 14.19 3.23 14 69686 0.1225 15.37 3.23 15 67135 0.1350 15.08 2.64 16 68921 0.1387 14.41 2.27 17 70524 0.1512 13.48 2.08 18 79354 0.1697 13.89 2.09 19 91718 0.1806 14.17 2.17 20 102153 0.1813 14.16 2.18 attr(,"class") [1] "cobbDouglasDeriv" > # t-values > margProductsObs$deriv / margProductsObs$variance^0.5 qLabor qVarInput land time 1 0.599 1.47 10.4 6.19 2 0.599 1.47 10.4 6.19 3 0.599 1.47 10.4 6.19 4 0.599 1.47 10.4 6.19 5 0.599 1.47 10.4 6.19 6 0.599 1.47 10.4 6.19 7 0.599 1.47 10.4 6.19 8 0.599 1.47 10.4 6.19 9 0.599 1.47 10.4 6.19 10 0.599 1.47 10.4 6.19 11 0.599 1.47 10.4 6.19 12 0.599 1.47 10.4 6.19 13 0.599 1.47 10.4 6.19 14 0.599 1.47 10.4 6.19 15 0.599 1.47 10.4 6.19 16 0.599 1.47 10.4 6.19 17 0.599 1.47 10.4 6.19 18 0.599 1.47 10.4 6.19 19 0.599 1.47 10.4 6.19 20 0.599 1.47 10.4 6.19 > > > # calculate optimal quantities of variable inputs > xCoef <- coef( estResult )[ 1:3 ] > zCoef <- coef( estResult )[ 4:5 ] > names( zCoef ) <- c( "d_1", "d_2" ) > optInput <- cobbDouglasOpt( pyName = "pOutput", + pxNames = c( "pLabor", "pVarInput" ), coef = xCoef, + data = germanFarms, xNames = c( "qLabor", "qVarInput" ), + zNames = c( "land", "time" ), zCoef = zCoef ) > print( optInput ) qLabor qVarInput 1 1.264 197 2 0.789 195 3 0.593 195 4 0.710 198 5 0.740 199 6 0.735 192 7 0.759 204 8 0.730 199 9 0.757 194 10 0.699 191 11 0.705 194 12 0.689 223 13 0.682 232 14 0.775 250 15 0.779 252 16 0.625 225 17 0.635 237 18 0.587 219 19 0.558 222 20 0.568 242 > > # determine optimal quantities of variable inputs using optim() > objFun <- function( xVal, obs = 1 ) { + tmpData <- germanFarms + tmpData$qLabor[ obs ] <- xVal[ 1 ] + tmpData$qVarInput[ obs ] <- xVal[ 2 ] + outp <- translogCalc( c( "qLabor", "qVarInput", "land", "time" ), + data = tmpData, coef = coef( estResult ) ) + profit <- germanFarms$pOutput[ obs ] * outp[ obs ] - + germanFarms$pLabor[ obs ] * xVal[ 1 ] - + germanFarms$pVarInput[ obs ] * xVal[ 2 ] + return( profit ) + } > optInputNum <- data.frame( qLabor = rep( NA, nrow( germanFarms ) ), + qVarInput = rep( NA, nrow( germanFarms ) ) ) > for( obs in 1:nrow( germanFarms ) ) { + optResult <- optim( + c( germanFarms$qLabor[ obs ], germanFarms$qVarInput[ obs ] ), + objFun, method = "L-BFGS-B", lower = 1e-10, + control = list( fnscale = -1 ), obs = obs ) + optInputNum[ obs, ] <- optResult$par + } > all.equal( optInput, optInputNum, check.attributes = FALSE, tolerance = 1e-5 ) [1] TRUE > > proc.time() user system elapsed 3.37 0.23 3.54