[R] condition has length > 1 for LL denominator
M. Tran
michellev.tran at gmail.com
Tue Nov 1 21:45:02 CET 2011
I have a dataset called "results" that looks like this:
arrive depart intercept
1 1 1
1 2 1
1 3 1
1 2 2
1 3 2
1 3 3
2 2 2
2 3 2
3 3 3
where arrive is the period of arrival, depart is the period of departure,
and intercept is the period in which that person was counted. I'm trying to
construct the denominator for a likelihood function using the following
function. For the first row in "results", for example, I want the
denominator to be the sum of all possible arrive/depart combinations an
interceptor in period 1 could observe: exp(P_1_1) + exp(P_1_2) + exp(P_1_3)
(i.e. P_arrive_depart).
get_denominator = function(intercept, periods_per_day)
{
denominator = array("(", nrow(results))
for (arrival in 1:periods_per_day)
{
for (departure in arrival:periods_per_day)
{
while (arrival <= intercept & intercept <= departure)
{
addition_to_denom = paste("P", arrival, departure, sep = "_")
if (nchar(denominator) == 1)
{
denominator = paste(denominator, "exp(", addition_to_denom, ")", sep =
"")
}
else
{
denominator = paste(denominator, " + exp(", addition_to_denom, ")", sep =
"")
}
}
}
}
denominator = paste(denominator, ")")
return(denominator)
}
denominator = get_denominator(intercept = results[,"intercept"],
periods_per_day = 3)
I'm getting the following warning message:
In if (arrival <= intercept & intercept <= departure) { ... :
the condition has length > 1 and only the first element will be used.
As written, the code gives me the denominator for a period 1 interceptor for
every single row!
I'm having trouble figuring out how I should re-write this code. Any
suggestions would be greatly appreciated.
--
View this message in context: http://r.789695.n4.nabble.com/condition-has-length-1-for-LL-denominator-tp3965365p3965365.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list