Error occuring in plotting thin plate spline model - r

library(mgcv)
#Input dataset
vo2max<-read.csv('C:/Users/Dell/Desktop/Paper(2009)/vo2max.csv')
model1 <- gam(VO2max ~ s(Load, Speed, m=4, bs=c("tp", data = vo2max)
x <- range(vo2max$Load)
x <- seq(x[1], x[2], length.out=14)
y <- range(vo2max$Speed)
y <- seq(y[1], y[2], length.out=14)
z <- outer(x,y,
function(Load,Speed)
predict(model1, data.frame(Load,Speed)))
p <- persp(x,y,z, theta=30, phi=30,
col="yellow",expand = 0.5,shade = 0.2, ticktype = "detailed",
xlab="Load", ylab="Speed", zlab="VO2max")
obs<- trans3d(vo2max$Load, vo2max$Speed,vo2max$VO2max,p)
pred<- trans3d(vo2max$Load, vo2max$Speed,fitted(model1),p)`enter code here`
points(obs, col="red",pch=16)
segments(obs$x, obs$y, pred$x, pred$y)
predict(model1)
The dataset contains 14 values. I need to plot a thin plate spline and predict the model,but I'm getting error in smooth construct. Whether I have to use any other library package. Help me in providing the appropriate code.

Related

Drawing 3D plot in R programming

library(mgcv)
model1 <- gam(EE ~ te(Load, Gradient, k=4, bs=c("tp", "tp")), data = dipasEE)
x <- range(dipasEE$Load)
x <- seq(x[1], x[2], length.out=20)
y <- range(dipasEE$Gradient)
y <- seq(y[1], y[2], length.out=20)
z <- outer(x,y,
function(Load,Gradient)
predict(model1, data.frame(Load,Gradient)))
p <- persp(x,y,z, theta=30, phi=30,
col="yellow",expand = 0.5,shade = 0.2, ticktype = "detailed",
xlab="Load", ylab="Gradient", zlab="EE")
obs<- trans3d(dipasEE$Load, dipasEE$Gradient,dipasEE$EE,p)
pred<- trans3d(dipasEE$Load, dipasEE$Gradient,fitted(model1),p)
points(obs, col="red",pch=16)
segments(obs$x, obs$y, pred$x, pred$y)
predict(model1)
pd <- data.frame(Load = seq(0, 21.5, by = 0.5), Gradient = 15)
predict(model1,newdata=pd)
Output:
[enter image description here][1]
# How to plot the points for newdata=pd in the 3d plot? Below is the Output for above code with Plots. Need to Point more plots for newdata=pd.
# 3 D Plot Graph
[1]: https://i.stack.imgur.com/bIMoY.png

I would like to visualize the third order interaction fitted with thin-plate regression splines

I am a beginner of R so it may be a simple question.
I am now trying to fit a 4-dimensional point using thin-plate regression splines. One variable is a target variable and three variables are an explanatory variable.
I made a model with third order interaction and fitted the data to this.
library(mgcv)
dat <- read.csv('../data//data.csv')
model <- gam(Y ~ s(x1, x2, x3), data=dat)
By giving x3, I want to visualize a three-dimensional graph of spline curve or estimated contour plot, but how do I do it?
It will be very helpful if you can answer.
Thanks.
This is the sample data.
n = 100
x1 <- runif(n, min = 0, max = 100)
x2 <- runif(n, min = 0, max = 100)
x3 <- runif(n, min = 0, max = 100)
Y = numeric(n)
for(i in 1:n){
Y[i] <- x1[i]**0.5*x2[i]**2*x3[i]/10000
}
dat = data.frame(Y=Y, x1=x1, x2=x2, x3=x3)
I do thin-plane regression spline using this dat.
model <- gam(Y ~ s(x1, x2, x3, k= 50), data=dat)
Then, I would like to obtain a fitting curve of three-dimensional thin-plane regression spline or contour plot estimated by regression spline when x3 = 25, for example.
To make a contour plot, you can use contour(x, y, z, ...). z is your data Matrix (in your case, Y[x1,x2, ], x and y are index vectors from 0 to 1 with a length of nrow(Y[x1,x2, ]) and ncol(Y[x1,x2, ]).
You should be able to use it similar to:
contour( x = seq(0, 1, length.out = length(x1)), y = seq(0, 1, length.out = length(x2)), z = Y[x1,x2, ] )
I found a solution with reference to the answer of d0d0.
n=100
const=25
x = y = seq(0, n, 1)
f = function(x,y){
dtmp <- data.frame(x1=(x), x2=(y), x3=(const))
pred <- predict.gam(model, dtmp)
}
z = outer(x, y, f)
contour(x,y,z)

Smoothing 3d plot in R

I made 3d plot in rgl.persp3d but I don't know how to smooth that to see trend. Or maybe next solution is to implement wireframe in rgl.persp3d (because I need this plot to be interactive). Please, help.
library(mgcv)
x<- rnorm(200)
y<- rnorm(200)
z<-rnorm(200)
tab<-data.frame(x,y,z)
tab
#surface wireframe:
mod <- gam(z ~ te(x, y), data = tab)
wyk <- matrix(fitted(mod), ncol = 20) #8 i 10 też ok
wireframe(wyk, drape=TRUE, colorkey=TRUE)
#surface persp3d
library(rgl)
library(akima)
z_interpolation <- 200
tabint <- interp(x, y, z)
x.si <- tabint$x
y.si <- tabint$y
z.si <- tabint$z
nbcol <- 200
vertcol <- cut(t, nbcol)
color = rev(rainbow(nbcol, start = 0/6, end = 4/6))
persp3d(x.si, y.si, z.si, col = color[vertcol], smooth=T)
So wireframe is neither smoothed nor interactive
...and rgl.persp3d is interactive but no smoothed. And I can't have both smoothed and interactive.
rgl just draws what you give it. You need to use mgcv as in your first example to do the smoothing, but you don't get a matrix of fitted values back at the end, so you'll want to use deldir to turn the results into a surface. For example,
library(mgcv)
x<- rnorm(200)
y<- rnorm(200)
z<-rnorm(200)
tab<-data.frame(x,y,z)
tab
#surface wireframe:
mod <- gam(z ~ te(x, y), data = tab)
library(rgl)
library(deldir)
zfit <- fitted(mod)
col <- cm.colors(20)[1 +
round(19*(zfit - min(zfit))/diff(range(zfit)))]
persp3d(deldir(x, y, z = zfit), col = col)
aspect3d(1, 2, 1)
This gives a nice smooth surface, for example
A simpler way, without the delaunay stuff:
library(mgcv)
x <- rnorm(200)
y <- rnorm(200)
z <- rnorm(200)
tab <- data.frame(x,y,z)
mod <- mgcv::gam(z ~ te(x, y), data = tab)
grid <- -5:5
zfit <- predict(mod, expand.grid(x = grid, y = grid))
persp3d(grid, grid, zfit)

`scatterplot3d`: can not add a regression plane to 3D scatter plot

I have created a 3d Scatterplot in R and want to add a regression plane. I have looked at code from the statmethods.net website, which can be very useful, and it worked. I then tried it with my own data and the plane did not show up.
library(scatterplot3d)
s3d <- scatterplot3d(Try$Visits, Try$Net.Spend, Try$Radio, pch=16, highlight.3d = TRUE, type = "h", main = "3D Scatterplot")
fit <- lm(Try$Visits ~ Try$Net.Spend +Try$Radio)
s3d$plane3d(fit)
I can not reproduce the issue with the following reproducible example:
set.seed(0)
x <- runif(20)
y <- runif(20)
z <- 0.1 + 0.3 * x + 0.5 * y + rnorm(20, sd = 0.1)
dat <- data.frame(x, y, z)
rm(x,y,z)
fit <- lm(z ~ x + y, data = dat)
library(scatterplot3d)
s3d <- scatterplot3d(dat$x, dat$y, dat$z, pch=16, highlight.3d = TRUE, type = "h", main = "3D Scatterplot")
s3d$plane3d(fit)
You should avoid $ in model formula. Use data argument instead:
fit <- lm(Visits ~ Net.Spend + Radio, data = Try)
Your z-variable(dependent variable) in the scatter plot is Try$Radio whereas in the regression model, the dependent variable is Try$Visits and this is causing confusion. The 3rd variable in the scatter plot argument is treated as the dependent variable R.

B-spline for non-polynomial splines in r

I intended to fit a 2D co-ordinate cloud, which resembles an incomplete ellipse. When I tried using bs() function from "splines" package in R to get the fitting curve (which is used to generate the B-spline basis matrix for a polynomial spline), it looked like this.
But what I intended was something like this.
To feed the parameters to bs() and plot it, I just followed this
mod <- lm(y ~ bs(x, knots = seq(0.1, 0.9, by = 0.1)))
pdat <- data.frame(x = seq(min(x), max(x), length = 100))
## predict for new `x`
pdat <- transform(pdat, yhat = predict(mod, newdata = pdat))
# plotting
plot(y ~ x)
lines(yhat ~ x, data = pdat, lwd = 2, col = "red")
Is there any way to achieve what I intended?
Thanks!

Resources