我有兴趣了解影响离开河流的鱼类比例的因素,如鱼苗,parr或smolt(总和为1)。我知道使用nnet :: multinom可以做到这一点很好,但是我想看看我是否可以使用mgcv包来适应这些与splines的关系。为此,我将使用3个类来拟合多项模型,其回归权重相对于当天观察到的该类的比例。我可以用mgcv拟合多项模型,然而,它似乎并不接受权重。请参阅下面的示例 - 输出与重量相同或不相同。有没有人知道是否可以在mgcv中实现的多项式gam中使用权重?
library(mgcv)
set.seed(6)
## simulate some data from a three class model
n <- 1000
f1 <- function(x) sin(3*pi*x)*exp(-x)
f2 <- function(x) x^3
f3 <- function(x) .5*exp(-x^2)-.2
f4 <- function(x) 1
x1 <- runif(n);x2 <- runif(n)
eta1 <- 2*(f1(x1) + f2(x2))-.5
eta2 <- 2*(f3(x1) + f4(x2))-1
p <- exp(cbind(0,eta1,eta2))
p <- p/rowSums(p) ## prob. of each category
cp <- t(apply(p,1,cumsum)) ## cumulative prob.
## simulate multinomial response with these probabilities
## see also ?rmultinom
y <- apply(cp,1,function(x) min(which(x>runif(1))))-1
## plot simulated data...
plot(x1,x2,col=y+3)
## now fit the model...
b <- gam(list(y~s(x1)+s(x2),~s(x1)+s(x2)),family=multinom(K=2))
# Try with weights
reg_weights <- sample(1:100, length(y),replace=T)
b_2 <- gam(list(y~s(x1)+s(x2),~s(x1)+s(x2)),family=multinom(K=2), weights =
reg_weights)
b; b_2 #identical
答案 0 :(得分:0)
更新 - 我认为VGAM软件包可以满足我的需求。这是一个有效的例子,使用VGAM的气体数据在每个班级创造%。
library(mgcv)
library(VGAM)
# Get pneumo data and log transform exposure time
pneumo <- transform(pneumo, let = log(exposure.time))
# Generate % in each class - note, not suggesting you do
# this if you have counts, just as an illustrative example if
# you have % in each class
props <- round(prop.table(as.matrix(pneumo[,c("normal", "mild",
"severe")]),margin=1)*100,0)
# Fit vgam mod with smoothed relationship with log exposure time
fit <- vgam(props ~ s(let), propodds, data = pneumo)
summary(fit)
plot(fit)