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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
broken.line<-function(ogg, term=NULL, link=TRUE, interc=TRUE, se.fit=TRUE, isV=FALSE, .vcov=NULL, .coef=NULL, ...){
#ogg: l'oggetto segmented
#term: una lista *nominata* con i valori rispetto a cui calcolare i fitted
# OPPURE una stringa per indicare la variabile segmented OPPURE NULL (se c'e' solo una variabile)
#is: 2 valori T/F per indicare se le variabili U e V nella matrice X, andrebbero sostituite con le versioni ind-smooth prima di calcolare var(X\hat\beta)
#...: argomenti da passare a vcov.segmented(): per esempio var.diff, is, p.df
if(length(isV)==1) isV<-c(FALSE,isV)
dummy.matrix<-NULL
dummy.matrix<-function(x.values, x.name, obj.seg, psi.est=TRUE, isV=FALSE, .coef=NULL){
#given the segmented fit 'obj.seg' and a segmented variable x.name with corresponding values x.values,
#this function simply returns a matrix with columns (x, (x-psi)_+, -b*I(x>psi))
#or ((x-psi)_+, -b*I(x>psi)) if obj.seg does not include the coef for the linear "x"
f.U<-function(nomiU, term=NULL){
#trasforma i nomi dei coeff U (o V) nei nomi delle variabili corrispondenti
#and if 'term' is provided (i.e. it differs from NULL) the index of nomiU matching term are returned
k<-length(nomiU)
nomiUsenzaU<-strsplit(nomiU, "\\.")
nomiU.ok<-vector(length=k)
for(i in 1:k){
nomi.i<-nomiUsenzaU[[i]][-1]
if(length(nomi.i)>1) nomi.i<-paste(nomi.i,collapse=".")
nomiU.ok[i]<-nomi.i
}
if(!is.null(term)) nomiU.ok<-(1:k)[nomiU.ok%in%term]
return(nomiU.ok)
}
estcoef <- if(is.null(.coef)) coef(obj.seg) else .coef
if(length(isV)==1) isV<-c(FALSE,isV)
n<-length(x.values)
#le seguenti righe selezionavano (ERRONEAMENTE) sia "U1.x" sia "U1.neg.x" (se "x" e "neg.x" erano segmented covariates)
#nameU<- grep(paste("\\.",x.name,"$", sep=""), obj.seg$nameUV$U, value = TRUE)
#nameV<- grep(paste("\\.",x.name,"$", sep=""), obj.seg$nameUV$V, value = TRUE)
nameU<-obj.seg$nameUV$U[f.U(obj.seg$nameUV$U,x.name)]
nameV<-obj.seg$nameUV$V[f.U(obj.seg$nameUV$V,x.name)]
#browser()
if(is.null(obj.seg$constr)){
diffSlope<-estcoef[nameU]
} else {
diffSlope<-drop(obj.seg$constr$invA.RList[[match(x.name, obj.seg$nameUV$Z)]]%*%estcoef[nameU])[-1]
}
est.psi<-obj.seg$psi[nameV, "Est."]
se.psi<-obj.seg$psi[nameV, "St.Err"]
if(any(is.na(se.psi))) stop("The St.Err. of psi is NA", call. = FALSE)
k<-length(est.psi)
PSI <- matrix(rep(est.psi, rep(n, k)), ncol = k)
SE.PSI <- matrix(rep(se.psi, rep(n, k)), ncol = k)
newZ<-matrix(x.values, nrow=n,ncol=k, byrow = FALSE)
dummy1<-if(isV[1]) (newZ-PSI)*pnorm((newZ-PSI)/SE.PSI) else (newZ-PSI)*(newZ>PSI) #pmax(newZ-PSI,0)
if(psi.est){
V<-if(isV[2]) -pnorm((newZ-PSI)/SE.PSI) else -(newZ>PSI) #ifelse(newZ>PSI,-1,0)
dummy2<- if(k==1) V*diffSlope else V%*%diag(diffSlope) #t(diffSlope*t(-I(newZ>PSI)))
colnames(dummy2)<- nameV
newd<-cbind(x.values,dummy1,dummy2)
colnames(newd)<-c(x.name,sub("psi","U", nameV), nameV)
#colnames(newd)[1]<- x.name
#colnames(newd)<-c(x.name,nameU, nameV)
} else {
newd<-cbind(x.values,dummy1)
colnames(newd)<-c(x.name, sub("psi","U", nameV))
#colnames(newd)[1]<- x.name
#colnames(newd)<-c(x.name,nameU)
}
#if(!x.name%in%names(estcoef)) newd<-newd[,-1,drop=FALSE]
#aggiungi (eventualmente) le colonne relative ai psi noti
all.psi<-obj.seg$indexU[[x.name]]
if(length(all.psi)!=k){
nomi.psi.noti<-setdiff(names(all.psi),nameU)
psi.noti<-setdiff(all.psi, est.psi)
PSI.noti <- matrix(rep(psi.noti, rep(n, length(psi.noti))), ncol = length(psi.noti))
nomi<-c(colnames(newd),nomi.psi.noti)
newZ<-matrix(newZ, nrow=nrow(newZ), ncol=length(psi.noti))
newd<-cbind(newd, (newZ-PSI.noti)*(newZ>PSI.noti))
colnames(newd)<-nomi
}
rownames(newd)<-NULL
return(newd)
} #end dummy.matrix()
#--------------
blockdiag <- function(...) {
args <- list(...)
nc <- sapply(args,ncol)
cumnc <- cumsum(nc)
## nr <- sapply(args,nrow)
## NR <- sum(nr)
NC <- sum(nc)
rowfun <- function(m,zbefore,zafter) {
cbind(matrix(0,ncol=zbefore,nrow=nrow(m)),m,
matrix(0,ncol=zafter,nrow=nrow(m)))
}
ret <- rowfun(args[[1]],0,NC-ncol(args[[1]]))
for (i in 2:length(args)) {
ret <- rbind(ret,rowfun(args[[i]],cumnc[i-1],NC-cumnc[i]))
}
ret
}
#--------------
f.U<-function(nomiU, term=NULL){
#trasforma i nomi dei coeff U (o V) nei nomi delle variabili corrispondenti
#and if 'term' is provided (i.e. it differs from NULL) the index of nomiU matching term are returned
k<-length(nomiU)
nomiUsenzaU<-strsplit(nomiU, "\\.")
nomiU.ok<-vector(length=k)
for(i in 1:k){
nomi.i<-nomiUsenzaU[[i]][-1]
if(length(nomi.i)>1) nomi.i<-paste(nomi.i,collapse=".")
nomiU.ok[i]<-nomi.i
}
if(!is.null(term)) nomiU.ok<-(1:k)[nomiU.ok%in%term]
return(nomiU.ok)
}
#-------------
estcoef<- if(is.null(.coef)) coef(ogg) else .coef
if(se.fit) {
covv<- if(is.null(.vcov)) vcov.segmented(ogg, ...) else .vcov
#---Dalla versione 1.2.0 (20/06/20) ho eliminato il controllo sotto per consentire l'utilizzo
#----di modelli che restituivano una cov con dimensione diversa dal numero dei coeff lineari (ad es., censReg)
#if(!all(dim(.vcov)==c(length(ogg$coef), length(ogg$coef)))) stop("Incorrect dimension of cov matrix", call. = FALSE)
if(!all(dim(covv)==c(length(estcoef), length(estcoef)))) stop("dimension of cov matrix and estimated coeffs do not match", call. = FALSE)
}
#browser()
nomeZ <- ogg$nameUV$Z
if(is.null(term)){
term <- nomeZ[1]
xvalues<-ogg$model[term]
} else {
if(is.character(term)) term<- ogg$model[term]
if(!is.list(term)) stop("term should be a named list")
if(!names(term)%in%nomeZ) stop("term is not a segmented variable")
xvalues<-term
term<-names(term)
}
n.seg<-1
# if(is.null(xvalues)){
# if(n.seg>1) stop("there are multiple segmented covariates. Please specify one.")
# xvalues<-ogg$model[nomeZ]
# }
# if(is.character(xvalues)){
# if(!xvalues %in% nomeZ) stop("'xvalues' is not a segmented covariate")
# xvalues<-ogg$model[xvalues]
# }
# nomeOK<-names(xvalues)
# if(length(nomeOK)>1) stop("Please specify one variable")
# if(!nomeOK %in% nomeZ) stop("'names(xvalues)' is not a segmented covariate")
nomi <- names(estcoef)
#nomiSenzaV <- nomiSenzaU <- nomi
#nomiSenzaU[match(nomeU, nomi)] <- ""
#nomiSenzaV[match(nomeV, nomi)] <- ""
idInterc<-grep("ntercept",names(estcoef))
ste.fit<-fit <- vector(mode = "list", length = 1)
n.seg<-1
#browser()
ind <- match(c(term,ogg$nameUV$U[grep(term, ogg$nameUV$U)]), nomi, 0)
ind<-ind[ind!=0]
indV <- match(c(ogg$nameUV$V[grep(term, ogg$nameUV$V)]), nomi, 0)
#Xfit<-dummy.matrix(unlist(xvalues), term, ogg, isV=FALSE, .coef=estcoef)
#if(se.fit)
X<-dummy.matrix(unlist(xvalues), term, ogg, isV=isV, .coef=estcoef)
#browser()
if(is.null(ogg$constr)){
cof <- estcoef[ind]
if(!term%in%names(estcoef)) X <-X[,-1,drop=FALSE]
} else {
#idU.i <- match(nomeU.i, names(estcoef))
cof<-drop(ogg$constr$invA.RList[[match(term, ogg$nameUV$Z)]]%*%estcoef[ind])
names(cof)<-c(term, paste("U",1:(length(cof)-1),".",term,sep="" ))
#estcoef<-append(estcoef[-ind], cof, after=ind[1]-1)
}
idV <- match(grep(term, ogg$nameUV$V, value = TRUE),colnames(X))
Xfit <- X[, -idV, drop=FALSE]
fit<-drop(Xfit%*%cof)
if(interc && length(idInterc)==1){
fit <- fit + estcoef[idInterc]
#if(se.fit)
X<-cbind(1,X)
ind <- c(idInterc, ind)
}
#browser()
if(se.fit) {
V <- covv[c(ind, indV), c(ind, indV)]
if(!is.null(ogg$constr)){
B <- ogg$constr$invA.RList[[match(term, ogg$nameUV$Z)]]
B <- do.call(blockdiag, list(diag(interc), B, diag(length(indV))))
V <- B %*% V %*% t(B)
}
#else {
# V <- vcov(object)
# X <- X[,colnames(V)]
#}
ste.fit <- sqrt(rowSums((X %*% V) * X)) #sqrt(diag(X%*%Var%*%t(X)))
if(inherits(ogg, what = "glm", FALSE) && !link) {
ste.fit <- ogg$family$mu.eta(fit)*ste.fit
fit <- ogg$family$linkinv(fit)
}
fit<-list(fit=fit, se.fit=ste.fit)
} else {
if(inherits(ogg, what = "glm", FALSE) && !link) fit <- ogg$family$linkinv(fit)
fit<-list(fit=fit)
}
return(fit)
}
|