[R] Invalid 'times' argument three-category ordered probit with maximum likelihood
jackle1o4
tmicka2 at gmail.com
Mon Nov 12 23:16:36 CET 2012
Hello,
First time poster here so let me know if you need any more information. I am
trying to run an ordered probit with maximum likelihood model in R with a
very simple model (model <- econ3 ~ partyid). Everything looks ok until i
try to run the optim() command and that's when I get " Error in rep(1,
nrow(x)) : invalid 'times' argument". I had to adapt the code from a 4
category likelihood and I have a suspicion that the problem is in there. The
data set consists of two variables with 558 observations. Econ3 is a 1-3
rating and partyid is a range from -3(Strong Democrat) to 3 (Strong
Republican). Here is the code I used:
setwd("C:/Users/Terry/Desktop/Terry/School/Fall 2012/ML/HW")
#Load Libraries
library(MASS)
library(tile)
library(simcf)
#Load Data
econrate <- read.csv("hw4econ3.csv", header=TRUE, sep=",")
attach(econrate)
#Ordered Probit Liklihood
llk.oprobit3 <- function(param, x, y) {
os <- rep(1, nrow(x))
x <- cbind(os, x)
b <- param[1:ncol(x)]
t2 <- param[(ncol(x)+1)]
xb <- x%*%b
p1 <- log(pnorm(-xb))
if (t2<=0) p2 <- -(abs(t2)*10000)
else p2 <- log(pnorm(t2-xb)-pnorm(-xb))
p3 <- log(1-pnorm(t2-xb))
-sum(cbind(y==1,y==2,y==3) * cbind(p1,p2,p3))
}
#Define Data
y <- econ3
x <- partyid
model <- (econ3 ~ partyid)
#Use optim directly
ls.result <- lm(y~x)
stval <- c(ls.result$coefficients, 1)
oprobit.result <- optim(stval, llk.oprobit3, method="BFGS", x=x, y=y,
hessian=T) ###Here is where it all breaks down
pe <- oprobit.result$par
vc <- solve(oprobit.result$hessian)
se <- sqrt(diag(vc))
ll <- -oprobit.result$value
Any help would be greatly appreciated. Thanks.
--
View this message in context: http://r.789695.n4.nabble.com/Invalid-times-argument-three-category-ordered-probit-with-maximum-likelihood-tp4649341.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list