library(SOP)
## An example of use of SOP package with tensor product B-splines in 2D
# Simulate the data
set.seed(123)
n <- 1000
sigma <- 0.1
x1 <- runif(n, -1, 1)
x2 <- runif(n, -1, 1)
f0 <- function(x1, x2) cos(2*pi*sqrt((x1 - 0.5)^2 + (x2 - 0.5)^2))
f <- f0(x1, x2)
y <- f + rnorm(n, 0, sigma)
dat <- data.frame(x1 = x1, x2 = x2, y = y)
# Theoretical surface
np <- 50
x1p <- seq(-1, 1, length = np)
x2p <- seq(-1, 1, length = np)
fp <- cos(2 * pi * sqrt(outer((x1p - 0.5) ^ 2, (x2p - 0.5) ^ 2, '+')))
image(x1p, x2p, matrix(fp, np, np), main = 'f(x1,x2) - Theor',
col = topo.colors(100))
# Fit the model
m0 <- sop(formula = y ~ f(x1, x2, nseg = 10), data = dat,
control = list(trace = FALSE))
summary(m0)
plot(m0, col = topo.colors(100))
## An example of use of SOP package with several smooth terms and Gamma distribution
# Simulate the data
set.seed(123)
n <- 1000
alpha <- 0.75
x0 <- runif(n)
x1 <- x0*alpha + (1-alpha)*runif(n)
x2 <- runif(n)
x3 <- x2*alpha + (1-alpha)*runif(n)
x4 <- runif(n)
x5 <- runif(n)
f0 <- function(x)2*sin(pi*x)
f1 <- function(x)exp(2*x)
f2 <- function(x) 0.2*x^11*(10*(1-x))^6+10*(10*x)^3*(1-x)^10
f <- f0(x0) + f1(x1) + f2(x2)
y <- rgamma(f,exp(f/4),scale=1.2)
df <- data.frame(y = y, x0 = x0, x1 = x1, x2 = x2, x3 = x3, x4 = x4, x5 = x5)
# Fit the model
m1 <- sop(formula = y ~ f(x0, nseg = 17) +
f(x1, nseg = 17) +
f(x2, nseg = 17) +
f(x3, nseg = 17) +
f(x4, nseg = 17) +
f(x5, nseg = 17), family = Gamma(link = log), data = df)
summary(m1)
plot(m1)
## An example of use of SOP package for the analysis of field trials experiments.
## Taken from the SpATS package.
require(SpATS)
data(wheatdata)
# Create factor variable for row and columns
wheatdata$R <- as.factor(wheatdata$row)
wheatdata$C <- as.factor(wheatdata$col)
# package SOP
m2 <- sop(formula = yield ~ colcode + rowcode +
f(col, row, nseg = c(10, 10)) +
rae(geno) + rae(R) + rae(C), data = wheatdata)
summary(m2)
plot(m2, col = topo.colors(100), pages = 1)
# Package SpATS: more adequate for this analysis.
# SpATS has been explicitly developed for the analysis field trials experiments.
m3 <- SpATS(response = "yield",
spatial = ~ SAP(col, row, nseg = c(10,10), degree = 3, pord = 2, center = TRUE),
genotype = "geno",
genotype.as.random = TRUE,
fixed = ~ colcode + rowcode, random = ~ R + C, data = wheatdata,
control = list(tolerance = 1e-06))
summary(m3)
plot(m3)
Run the code above in your browser using DataLab