Dear Masters,
I'm driving crazy with the lowess() function....
my intent is smoothing confidence intervals for predicted y values in a
linear model lm() setting
since in the predict() function there exists an option for predicting NA
values, I instead encounter problems when I fit a missing values x variable
to the predicted terms.....,impossible task!!!
I've been banging on my head all morning, I can't find a solution....
also using a loess() I can't get the function to keep the x missing values
with the result that it automaticaly drop 'em, so that the smoothing looks
like a mess in the plot ....
I attach my scripts, a function that would automatize the process
explained above
#d = a data.frame with some Na
#c1,c2 =column indexes
regr.plot<-function(d,c1,c2,
method=c("spearman","pearson"),titolo,xtag,ytag,livello){
test<-cor.test(d[,c2],d[,c1],method="spearman")
pval<-round(test$p.val,4)
corr<-round(test$estimate,4)
I<-predict(lm(d[,c1]~d[,c2]),newdata=d,interval="confidence",level=livello)
plot(d[,c2],d[,c1],xlab=as.character(xtag),ylab=as.character(ytag))
title(as.character(titolo),font=3)
abline(lm(d[,c1]~d[,c2]),lwd=2,col="red")
#lines(loess(I[,2]~d[,c2])#,data=d,control =loess.control(surface
"direct")
#,na.action=na.omit(d[,c2])
#,lty=2,lwd=2,col="blue")
#lines(loess(I[,3]~d[,c2])#,data=d,control =loess.control(surface
"direct")
#,na.action=na.omit(d[,c2])
#,lty=2,lwd=2,col="blue")
lines(lowess(d[,c2],I[,2]),lty=2,lwd=2,col="blue")
lines(lowess(d[,c2],I[,3]),lty=2,lwd=2,col="blue")
text(locator(1),as.expression(substitute(r==corr),list(corr=corr)),
cex=1.5,font=3)
if(pval==0){
text(locator(1),expression("p<0.001"),
cex=1.5,font=3)
}else{
text(locator(1),as.expression(substitute(p==pval),list(pval=pval)),
cex=1.5,font=3)
}
}
#THANK U 4 THE EVENTUAL ADVISE.....!!
Federico, Student of Statistics at Milano-Bicocca
[[alternative HTML version deleted]]