[R] Boxcox transformation

ruchi ruchitarg123 at gmail.com
Fri Apr 13 15:11:28 CEST 2012


I am time series data in Eviews to see the fitting of logistic and gompertz
model with my data.
I used NLS and then the Box cox transformation.

I need to see my graph of original data Vs predicted values / fiited
values on single graph . I cam do this when dealing only with NLS but I am
not able to plot it
with box cox. I read about    " lines.boxcox.fit " but not working. Please
let me know if anyone knows
Below is the code.

library(nlstools)
library(nlme)
library(nlrwr)

Use <-c(12.6,
15.1,
18.3,
21.99,
26.15,
29.205,
33.6,
37.4,
41.07,
44.94,
50.86,
58.5,
69.2,
78.49,
91.01,
105.4,
120.47,
135.79,
153.99,
172.23,
192.7,
212.51,
233.68,
258.23,
297.26,
328.23,
370.59,
421.58,
478.68,
527.62,
578.49,
641.73,
698.37,
737.33,
761.2 )
time  <-
c(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,242,25,26,27,28,29,30,31,32,33,34)
df <- data.frame(Use, time )
df
plot(df$time , df$Use, col = "blue", xlab="time((Time t=0 at Year 2003
Q1 (march))",ylab= "Users")
win.graph()
para0.st <- c(K=1062,
    a=3.998,     b=0.129 )
 fitGG <- nls(
  Use~ K*exp(-a*exp(-b*time )), df, start= para0.st,trace=T,
control=nls.control(maxiter=200))
 summary(fitGG)
// Plotting original Vs NLS fit data on same graph
plot(df$time , df$Use, xlim=c(0,40), ylim=c(0,1000), col = "blue",
xlab="Time (Year 2003)",ylab= "Use")
x <- seq(0, 40, length=100)

y2 <- predict(fitGG,data.frame(time =x))
lines(x,y2, lty="dotted", col="red")
nlsResiduals(fitGG)
resGG<-nlsResiduals(fitGG)

plot(resGG,which=0)
// now using box cox transformation
bcfitGG2<- boxcox.nls(fitGG)
 bcSummary(bcfitGG2)
coef(summary(bcfitGG2))
summary(bcfitGG2)
resGG1<-nlsResiduals(bcfitGG2)
 plot(resGG1,which=0)

/// trying to plot for the original Vs fitted box-cox values
plot(df$time , df$Use, xlim=c(0,40), ylim=c(0,1000), col = "blue",
xlab="Time (Year)",ylab= "Use")
x <- seq(0, 40, length=100)
y3 <- predict(bcfitGG2,data.frame(time =x))
lines(x,y3, lty="dotted", col="red")





--
View this message in context: http://r.789695.n4.nabble.com/Boxcox-transformation-tp4554769p4554769.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list