1 votes

Error in h (simpleError (msg, call))

I am doing my master's thesis and it is the first time that I use R. I must apply an estimate by Maximum Likelihood. The error I get is:

  - Error in h(simpleError(msg, call)) : 
         error in evaluating the argument 'object' in selecting a method for function 'logLik': only defined on a data frame with all numeric
   variables

This is what I have been applying step by step, the error is generated in the last command:

 Datos <- read.table("C:/Users/andre/Documents/Master Consultoria Economica/TFM/Base tfm.csv",header=TRUE, sep=";", na.strings="NA", dec=",", strip.white=TRUE, fileEncoding="UTF-8-BOM")

summary(Datos)
sapply(Datos, class)
attach(Datos)

##DEFINIR VARIABLES##

Ct=data.frame(VarCredito)

X1=data.frame(FonDisp,
              TAE,
              TasaMora,
              PrecioPet,
              IDEAC)
X2=data.frame(TAE,
              Paro,
              IPIM,
              Inflacion)

x1=as.matrix(X1)
x2=as.matrix(X2)  

 
#Definimos la función de verosimilitud
##########################################################
fmv <- function(y,xa,xb,sigma1,sigma2,
                a1,a2,a3,a4,a5,
                b1,b2,b3,b4 ) {
  beta1 <- data.frame(a1,a2,a3,a4,a5)
  beta2 <- data.frame(b1,b2,b3,b4)
  f1 <- 1/sqrt(2*pi*sigma1^2 ) * exp(-1/(2*sigma1^2 )*(Ct-x1%*%t(beta1))^2)
  f2 <- 1/sqrt(2*pi*sigma2^2 ) * exp(-1/(2*sigma2^2 )*(Ct-x2%*%t(beta2))^2)
  F1 <- numeric(nrow(Ct))
  F2 <- numeric(nrow(Ct))
  for (i in 1:nrow(Ct)){
    F1[i] <- pnorm(Ct[i,1], x1[i,]%*%t(beta1), sigma1, lower.tail = F)
    F2[i] <- pnorm(Ct[i,1], x2[i,]%*%t(beta2), sigma2, lower.tail = F)
  }
  -sum(log((f1*F2)+(f2*F1)),log=TRUE)
}


#PARAMETROS INICIALES MCO
summary(FIT <-
          lm(VarCredito~FonDisp+TAE+TasaMora+PrecioPet+IDEAC+-1))

summary(FIT2 <- lm(VarCredito~TAE+Paro+IPIM+Inflacion+-1))

plot(VarCredito,type="o",col="black")
lines(predict(FIT),type="o",col="blue")
lines(predict(FIT2),type="o",col="red")



#FUNCION MCO
fmc <- function(y,xa,xb,
                a1,a2,a3,a4,a5,
                b1,b2,b3,b4 ) {
  beta1 <- data.frame(a1,a2,a3,a4,a5)
  beta2 <- data.frame(b1,b2,b3,a4)
  sum((Ct-pmin(x1%*%t(beta1),x2%*%t(beta2), log=TRUE))^2)
}


library("bbmle")
class(Ct[,1])

p<-mle2(fmc,start=list(a1= -0.000003533,a2=0.009015,a3=-0.5944,a4=0.03418,a5=0.00001512,
                       b1= -0.0112861,b2= 0.6571781,b3= 0.0006255,b4= 0.0021346),
        data=list(y=Ct,xa=x1,xb=x2))

summary(p)
########################################################## 
#Tomamos 100 valores iniciales aleatorios con media los obtenidos por MCO 
##########################################################
asigma1<-rnorm(100,0.035,0.026) 
asigma2<-rnorm(100,0.035,0.026) 
aa1<-rnorm(100,coef(p)[1],1) 
aa2<-rnorm(100,coef(p)[2],1) 
aa3<-rnorm(100,coef(p)[3],0.0001) 
aa4<-rnorm(100,coef(p)[4], 0.0001) 
aa5<-rnorm(100,coef(p)[5],1)
ab1<-rnorm(100,coef(p)[6],1) 
ab2<-rnorm(100,coef(p)[7],0.0001) 
ab3<-rnorm(100,coef(p)[8],1)
ab4<-rnorm(100,coef(p)[9],0.0001)


#Vemos cuál de estos valores iniciales nos da una verosimilitud más alta
L <- numeric(100) 
for (i in 1:100){ 
m <- logLik(mle2(fmv,start=list(sigma1=asigma1[i],sigma2=asigma2[i], 
a1=aa1[i],a2=aa2[i],a3=aa3[i],a4=aa4[i],a5=aa5[i], 
b1=ab1[i],b2=ab2[i],b3=ab3[i],b4=ab4[i]), 
data=list(y=Ct,xa=x1,xb=x2))) 
L[i] <-m[1] 
} 

Later I have to apply which.max (L), but the error is generated in L and it does not let me. All variables are numeric.

1voto

Andrea Márquez Points 21

I have managed to resolve the error, apparently in f1 when the function calculates exp generates INF as a result because the numbers are too large, it was fixed by adding the function mprf and then calculating exp

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X