R语言 计算线性模型和其他GLM的GAIC时出错

ljsrvy3e  于 2023-06-19  发布在  其他
关注(0)|答案(2)|浏览(149)

我的数据集可以在这里找到:https://raw.githubusercontent.com/yuliaUU/test/main/test.csv

library(gamlss)
library(tidyverse)
data_final<- read_csv("https://raw.githubusercontent.com/yuliaUU/test/main/test.csv")

# Normal model with log transformation 
model_1 <-  gamlss(log(Abundance) ~ salinity*avrg_dep, data = data_final, family = NO())
# log normal model 
model_2 <- gamlss(Abundance ~  salinity*avrg_dep, data = data_final,  family = LOGNO())
#  Model with inverse gaussian distribution
model_3 <- gamlss(Abundance ~ salinity*avrg_dep, data = data_final,  family = IG())
# Gamma model
model_4 <- gamlss(Abundance ~ salinity*avrg_dep,  data = data_final, family = GA())

我想用GAIC来比较模型之间的差异,但第一个模型的GAIC值与其他模型相差甚远
我读到:
为确保具有转换响应的线性模型的GAIC相当,使用转换对数似然乘以雅可比矩阵,并手动重新计算GAIC。
我试着用下面的方法来做:

Jacobian <- 1/abs(data_final$Abundance)
# Calculate fitted values (on the log scale)
fitted_values_log <- predict(model_1)

# Calculate residuals manually (on the log scale)
residuals_transformed <- log(data_final$Abundance) - fitted_values_log

# Calculate standard deviation of the residuals
sd_residuals_transformed <- sd(residuals_transformed)

# Transformed log-likelihood calculation
log_likelihood_transformed <- sum(dnorm(log(data_final$Abundance), mean=fitted_values_log, sd=sd_residuals_transformed, log=TRUE) * Jacobian)

# Calculate degrees of freedom: number of parameters in the model
df <- length(coef(model_1))

# Manually calculate GAIC
GAIC_transformed <- -2 * log_likelihood_transformed + 2 * df
GAIC_transformed

但是产生的价值太低了,所以我想我在某个地方犯了一个错误

brccelvz

brccelvz1#

最简单的答案是在gamlss中显式拟合对数正态分布,即家族=LOGNO
一个更一般的答案,它适用于真实的线上的正态分布以外的分布,例如:TF,是创建相应的logTF分布:
gen.Family(“TF”,type=“log”)
然后在游戏中使用
家族=logTF

kh212irz

kh212irz2#

# Model 1: Log-transformed response with lm()
model_1 <- lm(log(Abundance) ~ salinity * avrg_dep, data = data_final)

# Calculate log-likelihood of the model
logL <- logLik(model_1)

# Adjust the log-likelihood using the Jacobian for a log transformation
adjusted_logL <- logL + sum(log(1/data_final$Abundance))

# Count the number of parameters in the model (including intercept)
k <- length(coef(model_1))

# Get sample size
n <- length(model_1$residuals)

# Compute GAIC with adjusted log-likelihood
GAIC_adjusted <- -2*adjusted_logL + 2*k + 2*k*(k+1)/(n-k-1)

print(GAIC_adjusted)

相关问题