Stocastic Frontier Analysis - r

I am currently using R for my final thesis. I am using the frontier library to run my data base. When I tried to run R, it show me this error message
" NA/NaN/Inf in foreign function call (arg 29)"
I realize the problem should be coming from the YEAR variable in my database. Any idea how to solve the problem? Thank you very much!
Code That I am using:
airlineCost <- sfa (log( airlinedb$Total.Cost ) ~ log( airlinedb$Price.of.Labor ) + log( airlinedb$Price.of.Capital )+
log( airlinedb$Price.of.Fuel) + log( airlinedb$RATK )+airlinedb$YEAR+airlinedb$Half.Year
+ airlinedb$YEAR*log( airlinedb$Price.of.Capital)+airlinedb$YEAR*log( airlinedb$Price.of.Fuel)+airlinedb$YEAR*log( airlinedb$Price.of.Labor)+airlinedb$YEAR*log( airlinedb$RATK)
+ airlinedb$Half.P.Fuel +airlinedb$Price.of.Labor +airlinedb$half.p.cap+airlinedb$half.RATK+
log(airlinedb$Price.of.Labor)*log(airlinedb$Price.of.Capital)+log(airlinedb$Price.of.Capital)*log(airlinedb$Price.of.Fuel)+log(airlinedb$Price.of.Labor)*log(airlinedb$Price.of.Fuel)
+log(airlinedb$RATK)*log(airlinedb$Price.of.Fuel)+log(airlinedb$RATK)*log(airlinedb$Price.of.Labor)+log(airlinedb$RATK)*log(airlinedb$Price.of.Capital), data=airlinedb, ineffDecrease = FALSE, truncNorm = FALSE, timeEffect = FALSE)
summary(airlineCost)
The following is the link to a small sample of data
https://drive.google.com/file/d/0B52JA3mxOm9DQlNyMWU4SW1qbnM/view?usp=sharing

Related

How to implement your own nonlinear function in nlmer in R?

I am trying to implement a new nonlinear function to use in nlmer function in lme4 package. But I'm not sure what the problem is. This is the first time I'm trying to use nlmer but I'm following all the instructions I've found on the internet. The first error is about my dataframe.
data <- read.csv(paste("C:/Users/oguz/Desktop/Runs4SiteModels/db/", "DB4NLSiteModel", Periods[i],".txt", sep=""), sep = "", header = TRUE)
psa_rock <- data$PSAr
nparams <- c("c")
nonl_fn <- deriv(~ log(( psa_rock + c)/c),
namevec = c("c"),
function.arg=c("c", psa_rock))
fm <- nlmer(log(data$PSAm) ~ nonl_fn(c, psa_rock) ~ 1 + data$M1 + data$M3 + data$M85 + data$Nflag + data$Rflag + data$FDepth +
data$Dist1 + data$Dist3 + data$VN + (exp(-1*exp(2*log(data$Vs)- 11)) * log((data$PSAr + c) / c) ) +
(1|data$EQID) + (1|data$STID), data=data, start=c(c=0.1))
When I run this code, I'm getting the following error:
Error in model.frame.default(data = data, drop.unused.levels = TRUE, formula = log(data$PSAm) ~ :
invalid type (list) for variable 'data'
which I wasn't getting it while using lmer function (of course without the nonlinear function). That's why I'm thinking my problem is not about my dataframe.
Other issue that I couldn't stop thinking about, the part in the fixed-effects:
(exp(-1*exp(2*log(data$Vs)- 11)) * log((data$PSAr + c) / c) )
as you can see my nonlinear function also takes a part in my fixed-effects formula and I'm not quite sure how to implement that. I hope my way is correct but because of my first problem, I couldn't find an opportunity to test that.

Trying to do a reset test in Rstudio

I'm trying to do a reset test in Rstudio. Im new to this.
This is my code:
model1 <- lm(inntekt ~ arbeidstid + kvinne + alder + leder + utd_hoy, data = d)
summary(model1)
install.packages("lmtest")
library("lmtest")
resettest(lm, power = 2:3, type = "regressor", data = d)
When i am trying to run the test i get this:
resettest(lm, power = 2:3, type = "regressor", data = d)
Error: object of type 'closure' is not subsettable
Can somebody pleas help me? I am happy for all answers :)
May be because you should write name of lm object, not just lm :
resettest(model1, #replace lm by model1
power = 2:3, type = "regressor", data = d)

Error in sensitivity analysis (medsens): Nonconformable Arrays

I am trying to run sensitivity analysis for a mediation test I ran and would appreciate feedback from anyone who might be able to identify the source of the error generated.
Mediation:
model.m1 <- lm(cagr_2006_2008 ~ log_pdens_06 + closeness_06 + rainfall_diff_06 + historical_drought+pct_ag_land + ag_prox + cagr_1990_2000 + historical_drought*ag_prox, data=lnd_data_c1_mediate)
summary(model.m1)
model.y1 <- glm(protest_sum_1_logit ~ log_total_pop_10 + cagr_2006_2008 + historical_drought + rainfall_diff_06, family=binomial(link="probit"), data=lnd_data_c1_mediate)
summary(model.y1)
out.1_moderate <- mediate(model.m1, model.y1, sims=1000, boot=TRUE, treat="rainfall_diff_06", mediator="cagr_2006_2008", control.value = -57.1, treat.value = 0, dropobs = TRUE)
Sensitivity Analysis:
sens.1 <- medsens(out.1_moderate, rho.by=0.05, sims=1000)
summary(sens.1)
## Error in Mmodel.coef.sim * (rho12.sim/sigma.2.sim) %x% t(rep(1, y.k - :
## non-conformable arrays
My code base and data sets are available here. (If you download Regression and Mediation Analyses.R with lnd_data_c1_mediate.csv and lnd_data.csv, and select the csv file locations when prompted, the code should run smoothly. The sensitivity analyses conducted are all grouped together at the end of the file.)
Thank you very much for any information you can provide!

R "Error in terms.formula" using GA/genalg library

I'm attempting to create a genetic algorithm (not picky about library, ga and genalg produce same errors) to identify potential columns for use in a linear regression model, by minimizing -adj. r^2. Using mtcars as a play-set, trying to regress on mpg.
I have the following fitness function:
mtcarsnompg <- mtcars[,2:ncol(mtcars)]
evalFunc <- function(string) {
costfunc <- summary(lm(mtcars$mpg ~ ., data = mtcarsnompg[, which(string == 1)]))$adj.r.squared
return(-costfunc)
}
ga("binary",fitness = evalFunc, nBits = ncol(mtcarsnompg), popSize = 100, maxiter = 100, seed = 1, monitor = FALSE)
this causes:
Error in terms.formula(formula, data = data) :
'.' in formula and no 'data' argument
Researching this error, I decided I could work around it this way:
evalFunc = function(string) {
child <- mtcarsnompg[, which(string == 1)]
costfunc <- summary(lm(as.formula(paste("mtcars$mpg ~", paste(child, collapse = "+"))), data = mtcars))$adj.r.squared
return(-costfunc)
}
ga("binary",fitness = evalFunc, nBits = ncol(mtcarsnompg), popSize = 100, maxiter = 100, seed = 1, monitor = FALSE)
but this results in:
Error in terms.formula(formula, data = data) :
invalid model formula in ExtractVars
I know it should work, because I can evaluate the function by hand written either way, while not using ga:
solution <- c("1","1","1","0","1","0","1","1","1","0")
evalFunc(solution)
[1] -0.8172511
I also found in "A quick tour of GA" (https://cran.r-project.org/web/packages/GA/vignettes/GA.html) that using "string" in which(string == 1) is something the GA ought to be able to handle, so I have no idea what GA's issue with my function is.
Any thoughts on a way to write this to get ga or genalg to accept the function?
Turns out I didn't consider that a solution string of 0s (or indeed, a string of 0s with one 1) would cause the internal paste to read "mpg ~ " which is not a possible linear regression.

Creating lavaan and mirt models permuations of varying size and length

hoping someone can offer some guidance here.
I'm creating a multivariate simulation using the simDesign package, I am varying the number of factors as well as items that load on each factor. I would like to write a command that identifies the number of factors present in factornumbers and assigns the appropriate items to them (no cross loading). I will be testing all combinations of the conditions below and more, and I would like to have a model command that acknowledge the iterations of differing models, so I don't have to write multiple model statements.
factornumbers<-c(1,2,3,5)
itemsperfactor<-c(5,10,30)
What lavaan and mirt are looking for is below:
mirtmodel<-mirt.model('
F1=1-15
F2=16-30
MEAN=F1,F2
COV=F1*F2')
lavmodel <- ' F1=~ Item_1 + Item_2 + Item_3 + Item_4 + Item_5 + Item_6 + Item_7 + Item_8 + Item_9 + Item_10 + Item_11 + Item_12 + Item_13 + Item_14 + Item_15
F2=~ Item_16 + Item_17 + Item_18 + Item_19 + Item_20 + Item_21 + Item_22 + Item_23 + Item_24 + Item_25 + Item_26 + Item_27 + Item_28 + Item_29 + Item_30'
The simDesign package offers this example, I would like to expand on it but I'm not sure I have the know-how:
lavmodel<-paste0('F=~ ', paste0(colnames(dat)[1L], ' + '),
paste0(colnames(dat)[-1L], collapse = ' + '))
What I would like is a single mirt and lavaan command that finds the number of factors specified in the factornumbers command and assigns the correct items specified in the data as well as itemsperfactor.
EDIT:
I would like the model identification to pick up on which factor & item structure is in use for that condition and fill in the model identification with the correct information.
For Example:
mirtmodel<-mirt.model('
F1=1-1
F2=6-10
F3=11-15
F4=16-20
F5=21-25
MEAN=F1,F2,F3,F4,F5
COV=F1*F2*F3*F4*F5')
Or
mirtmodel<-mirt.model('
F1=1-30
F2=31-60
MEAN=F1,F2
COV=F1*F2')
And also the corresponding lavaan models.
The idea here is to paste different strings together so that the condition input (row of the respective Design object) is all that is required to construct a suitable model specification string. Generating syntax for simulations is arguably the most annoying part of simulations, but at least in R there are a good number of helpful string operations (plus, packages like stringr).
Here's my interpretation of what you are currently looking for using base R functions.
library(SimDesign)
library(mirt)
Design <- createDesign(factornumbers = c(1,2,3,5),
itemsperfactor = c(5,10,30))
gen_syntax_mirt <- function(condition){
fn <- with(condition, factornumbers)
ipf <- with(condition, itemsperfactor)
nitems <- fn * ipf
maxloads <- sort(seq(nitems, ipf, length.out = fn))
minloads <- c(1, maxloads[-length(maxloads)] + 1)
fnames <- paste0('F', 1:fn)
df <- cbind(fnames, ' = ', minloads, '-', maxloads)
s1 <- apply(df, 1, paste0, collapse = '')
s2 <- paste0('MEAN = ', paste0(fnames, collapse = ','))
s3 <- paste0('COV = ', paste0(fnames, collapse = '*'))
ret <- paste0(c(s1, s2, s3), collapse = '\n')
mirt.model(ret)
}
gen_syntax_mirt(Design[1,])
gen_syntax_mirt(Design[10,])
The input to this function is a single row from the Design input to runSimulation(), so you can see here that it will work just fine. Do something similar for lavaan's syntax and you'll be set.

Resources