Showing posts with label GAM plotting. Show all posts
Showing posts with label GAM plotting. Show all posts

Friday, January 18, 2019

GAM plotting

www.r-languagestatistics.co

GAM plotting

x <- timeCalendar()
> y <- rnorm(12)
> plot(x, y, type = "l")
> points(x, y, pch = 19, col = "red")
> plot(x, y, type = "l", xaxt = "n")
> axis.timeDate(1, at = x[c(1, 3, 5, 7, 9, 11)], format = "%b")
> axis.timeDate(1, at = x[12], format = "%Y")
> library("mgcv", lib.loc="C:/Program Files/R/R-3.5.0/library")
Loading required package: nlme
This is mgcv 1.8-23. For overview type 'help("mgcv-package")'.
Warning message:
package ‘nlme’ was built under R version 3.5.2
> library(mgcv)
> set.seed(0)
> f1 <- function(x) {exp(2 * x)}
> f2 <- function(x) {
+     0.2*x^11*(10*(1-x))^6+10*(10*x)^3*(1-x)^10
+ }
> f3 <- function(x) {x*0}
> n<-200
> sig2<-4
> x0 <- rep(1:4,50)
> x1 <- runif(n, 0, 1)
> x2 <- runif(n, 0, 1)
> x3 <- runif(n, 0, 1)
> e <- rnorm(n, 0, sqrt(sig2))
> y <- 2*x0 + f1(x1) + f2(x2) + f3(x3) + e
> x0 <- factor(x0)
> b<-gam(y~x0+s(x1)+s(x2)+s(x3))
> plot(b,pages=1,residuals=TRUE,all.terms=TRUE,shade=TRUE,shade.col=2)
> plot(b,pages=1,residuals=TRUE,all.terms=TRUE,shade=TRUE,shade.col=7)
> plot(b,pages=1,seWithMean=TRUE)
> plot(b,pages=1,seWithMean=TRUE,clo=109)
There were 27 warnings (use warnings() to see them)
> plot(b,pages=1,seWithMean=TRUE,shade.col=7)
There were 50 or more warnings (use warnings() to see the first 50)
> termplot(b,terms="x0",se=TRUE)
> op <- par(mfrow=c(2,2),bg="blue")
> x <- 0:1000/1000
> for (i in 1:3) {
+     plot(b,select=i,rug=FALSE,col="green",
+          col.axis="white",col.lab="white",all.terms=TRUE)
+     for (j in 1:2) axis(j,col="white",labels=FALSE)
+     box(col="white")
+     eval(parse(text=paste("fx <- f",i,"(x)",sep="")))
+     fx <- fx-mean(fx)
+     lines(x,fx,col=2) ## overlay `truth' in red
+ }
> for (i in 1:3) {
+     plot(b,select=i,rug=FALSE,col="green",
+          col.axis="white",col.lab="white",all.terms=TRUE)
+     for (j in 1:2) axis(j,col="white",labels=FALSE)
+     box(col="white")
+     eval(parse(text=paste("fx <- f",i,"(x)",sep="")))
+     fx <- fx-mean(fx)
+     lines(x,fx,col=7) ## overlay `truth' in red
+ }
> for (i in 1:3) {
+     plot(b,select=i,rug=FALSE,col="green",
+          col.axis="white",col.lab="white",all.terms=TRUE)
+     for (j in 1:2) axis(j,col="white",labels=FALSE)
+     box(col="white")
+     eval(parse(text=paste("fx <- f",i,"(x)",sep="")))
+     fx <- fx-mean(fx)
+     lines(x,fx,col=9) ## overlay `truth' in red
+ }
> dat <- gamSim(1,n=400)
Gu & Wahba 4 term additive model
> b1 <- gam(y~te(x0,x1,x2,d=c(1,2),k=c(5,15))+s(x3),data=dat)
> plot(b1,pages=1)

Black-Scholes formula-R

 Black-Scholes formula-R > BlackScholes <- function(TypeFlag = c("c", "p"), S, X, Time, r, b, sigma) { TypeFla...