I'm trying to predict a nls fit in R. My values are intensities (y) and excitation energy (x). My code is the following:
mydata<-read.delim("TrapDepthExcSpec.txt",
header = TRUE)
x <- mydata[1:5,1]
y <- mydata[1:5,2]
plot(x,y)
k <- 65
Et <- 2.16
m0 <-0.7
test <- k*((x-Et)^(3/2))/(x*((x-(Et*(1-(m0/0.79))))^2))
lines(x,test)
EtFit <- function(x, k, Et, m0) k*((x-Et)^(3/2))/(x*((x-(Et*(1- (m0/0.79))))^2))
FitEt <- nls(y~EtFit(x, k, Et, m0),
start = list(k = 65, Et = 2.16, m0 = 0.7),
control = list(maxiter = 500),
algorithm = "port")
res <- coef(FitEt)
RES <- res[1]*((x-res[2])^(3/2))/(x*((x-(res[2]*(1-(res[3]/0.79))))^2))
lines(x,RES,col="red")
xall=mydata[,1]; yall=mydata[,2]
plot(xall,yall, col = "gray48", pch = 20)
lines(x,RES,col="red")
### predict nls ###
NewData <- as.matrix(cbind(x, RES))
colnames <- (NewData)
colnames(NewData) <- c("x", "y")
predict(FitEt, NewData, se.fit = TRUE, scale = NULL, df = Inf, interval = c("none", "confidence", "prediction"), level = 0.95)
My problem is that it always just results in the fitted values and does not predict new ones.
I know that there is already a question like this in here, but it never got answered so that it works.
Thank you very much for your help!