-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.R
32 lines (29 loc) · 1.2 KB
/
functions.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
## Fits a glm and returns the coeficients fro a regular time series, which is a line of an data.frame "dados"
fitP <- function(i, dados, family=poisson){
x <- 0:(length(dados[i,])-1)
y <- dados[i,]
fit <- glm(y~x, family=family)
ci <- confint(fit)
results <- c(coef(fit),ci[1,], ci[2,])
names(results) <- c("intercept", "coef", "int.low", "int.upp", "coef.low", "coef.upp")
results
}
## Fits a glm and returns the coeficientes fom a zoo object with time series
fitP.zoo <- function(zoo.obj, family=poisson, only.coef=TRUE){
ndias <- rev(max(time(zoo.obj)) - time(zoo.obj))
fit <- glm(zoo.obj ~ ndias, family = family)
if(only.coef){
ci <- confint(fit)
results <- c(coef(fit),ci[1,], ci[2,])
names(results) <- c("intercept", "coef", "int.low", "int.upp", "coef.low", "coef.upp")
}
else
results <- fit
return(results)
}
## Corta uma seria temporal da classe zoo, tirando todos os valores anteriores ao primeiro valor igual
## a um certo limite (n.casos). Para fazer a serie a partir do dia zero
diazero <- function(zoo.obj, limite){
dia.zero <- min(which(zoo.obj>=limite, arr.ind=TRUE))
zoo.obj[dia.zero:length(zoo.obj)]
}