function1
functions1.r — 1.1 KB
File contents
# Define a function to evaluate the fit of different sigma vectors. ssesigma<-function(sigmavec){ sigma<-c(sigmavec[1],sigmavec[2],sigmavec[3],rep(sigmavec[4],length(mu)-3)) fit<-rep(0,130) for(lgrp in 1:130){ fit[lgrp]<-sum(pi*(pnorm((lgrp+0.5-mu)/sigma)-pnorm((lgrp-0.5-mu)/sigma))) } sse<-sum((dat-fit)^2) #plot(lgrps,dat,type='b',lwd=2) #lines(lgrps,fit,type='l',lwd=2,col="red") #cat("SSE=",sse,"\n") return(sse) } # Define a function to evaluate the fit of different vectors of proportions sseprop<-function(pvec){ # The pvector is input - the mu and sigma are fixed pi<-c(pvec,1-sum(pvec)) # na-1 values - the last is computed pi<-abs(pi) # Make sure all are positive pi<-pi/sum(pi) # Readjust, just in case fit<-rep(0,130) # Compute the fitted values for(lgrp in 1:130){ fit[lgrp]<-sum(pi*(pnorm((lgrp+0.5-mu)/sigma)-pnorm((lgrp-0.5-mu)/sigma))) } sse<-sum((dat-fit)^2) #plot(lgrps,dat,type='b',lwd=2) #lines(lgrps,fit,type='l',lwd=2,col="red") #cat("SSE=",sse,"\n") return(sse) }