Generalizability: https://apps.mathpsy.uni-tuebingen.de/fw/generalizability/
par(mfrow = 1:2, mai = c(.6, .6, .3, .1), mgp = c(2, .7, 0))
dat <- data.frame(y = stevens(sd = 15), x = 1:6)
s <- nls(y ~ a*x^b, dat)
f <- nls(y ~ a*log(x + b), dat, lower = c(b = -.99), algorithm = "port")
plot(y ~ x, dat, main = "Stevens data")
predict(s) |> lines(col = "darkblue")
predict(f) |> lines(lty = 2, col = "darkblue")
legend("topleft", c(expression(a %.% x^b), expression(a %.% log(x + b))),
col = "darkblue", lty = 1:2, bty = "n")
dat <- data.frame(y = fechner(), x = 1:6)
s <- nls(y ~ a*x^b, dat)
f <- nls(y ~ a*log(x + b), dat, lower = c(b = -.99), algorithm = "port")
plot(y ~ x, dat, main = "Fechner data")
predict(s) |> lines(col = "darkblue")
predict(f) |> lines(lty = 2, col = "darkblue")set.seed(1029)
## Stevens data
aic1 <- replicate(2000, {
dat <- data.frame(y = stevens(sd = 15), x = 1:6)
c(s = AIC(nls(y ~ a*x^b, dat,
control = nls.control(warnOnly = TRUE))),
f = AIC(nls(y ~ a*log(x + b), dat,
control = nls.control(warnOnly = TRUE),
lower = c(b = -.99), algorithm = "port")))
})
## Fechner data
aic2 <- replicate(2000, {
dat <- data.frame(y = fechner(), x = 1:6)
c(s = AIC(nls(y ~ a*x^b, dat,
control = nls.control(warnOnly = TRUE))),
f = AIC(nls(y ~ a*log(x + b), dat,
control = nls.control(warnOnly = TRUE),
lower = c(b = -.99), algorithm = "port")))
})
tab <- list(
AIC = cbind(stevens = rowMeans(aic1),
fechner = rowMeans(aic2)),
"%" = cbind(stevens = c(mean(aic1["s", ] < aic1["f", ]),
mean(aic1["f", ] < aic1["s", ])),
fechner = c(mean(aic2["s", ] < aic2["f", ]),
mean(aic2["f", ] < aic2["s", ]))) * 100
) |> simplify2array()
names(dimnames(tab)) <- c("Model fitted", "Data from", "crit")
ftable(tab, col.vars = c("Data from", "crit")) |> round(2)## Data from stevens fechner
## crit AIC % AIC %
## Model fitted
## s 51.35 85.30 4.90 43.15
## f 55.58 14.70 4.70 56.85
## data {
## int<lower=1> N;
## vector[N] y;
## vector[N] x;
## }
## parameters {
## real a;
## real b;
## real<lower=0> sigma;
## }
## model {
## a ~ normal(2, 1);
## b ~ normal(2, 1);
## sigma ~ normal(0, 2);
## y ~ normal(a * x^b, sigma);
## }
## generated quantities {
## vector[N] log_lik;
## for (i in 1:N)
## log_lik[i] = normal_lpdf(y[i] | a * x[i]^b, sigma);
## }
## data {
## int<lower=1> N;
## vector[N] y;
## vector[N] x;
## }
## parameters {
## real a;
## real<lower=1> b;
## real<lower=0> sigma;
## }
## model {
## a ~ normal(2, 1);
## b ~ normal(5, 1);
## sigma ~ normal(0, 2);
## y ~ normal(a * log(x + b), sigma);
## }
## generated quantities {
## vector[N] log_lik;
## for (i in 1:N)
## log_lik[i] = normal_lpdf(y[i] | a * log(x[i] + b), sigma);
## }
library(rstan)
options(mc.cores = 4)
rstan_options(auto_write = TRUE)
## Stevens data
winner <- character(100)
for(i in seq_along(winner)) {
dat <- data.frame(y = stevens(sd = 15), x = 1:6)
s <- stan("stevens.stan", data = c(dat, N = nrow(dat)),
chains = 1, iter = 1000)
f <- stan("fechner.stan", data = c(dat, N = nrow(dat)),
chains = 1, iter = 1000)
l1 <- loo(s)
l2 <- loo(f)
winner[i] <-
loo::loo_compare(list(stevens = l1, fechner = l2)) |> rownames() |> _[1]
}
table(winner)
# stevens
# 100
## Fechner data
winner <- character(100)
for(i in seq_along(winner)) {
dat <- data.frame(y = fechner(), x = 1:6)
s <- stan("stevens.stan", data = c(dat, N = nrow(dat)),
chains = 1, iter = 1000)
f <- stan("fechner.stan", data = c(dat, N = nrow(dat)),
chains = 1, iter = 1000)
# print(s, pars = c("a", "b", "sigma"), probs = c(.025, .975))
# print(f, pars = c("a", "b", "sigma"), probs = c(.025, .975))
l1 <- loo(s)
l2 <- loo(f)
winner[i] <-
loo::loo_compare(list(stevens = l1, fechner = l2)) |> rownames() |> _[1]
}
table(winner)
# fechner stevens
# 85 15 library(lme4)
## Rasch model (participants as random effects)
m1 <- glmer(r2 ~ 0 + item + (1 | id), VerbAgg, binomial)
## Linear logistic test model (LLTM)
m2 <- glmer(r2 ~ 0 + btype + mode + situ + (1 | id),
VerbAgg, binomial)
## LLTM with homoscedastic error
m3 <- glmer(r2 ~ 0 + btype + mode + situ + (1 | id) + (1 | item),
VerbAgg, binomial)## AIC BIC
## 8128.519 8301.864
## m1 m3 m2
## 8128.519 8163.826 8249.935
## m3 m2 m1
## 8212.362 8291.537 8301.864