Weights in gam

There are a lot of redundant observations in my dataset (but every observation should be considered). Therefore, I am considering using the “weights” option in GAM because it greatly reduces the computation time.

Function

gam(in the package mgcv) explains that they are "equivalent" (from ?gamargument weights):

"Note that weight 2, for example, is equivalent to having twice made the same observation."

But that does not seem right.

yy = c(5,2,8,9)
xx = 1:4
wgts = c(3,2,4,1)
yy2 = rep(yy, wgts)
xx2 = rep(xx, wgts)
mod1 = gam(yy2 ~ xx2)
mod2 = gam(yy ~ xx, weights = wgts)
mod3 = gam(yy ~ xx, weights = wgts / mean(wgts))

predict(mod1,data.frame(xx2=1:4))
predict(mod2,data.frame(xx=1:4))
predict(mod3,data.frame(xx=1:4))

The ratings are the same in all three models. The standard error is the same for models 2 and 3, but differs in model 1. GCV differs in all three models.

, GCV . , , ? ?

+4
1

GAM. gam , gam , lm. , . , GLM GAM. lm:

yy <- c(5,2,8,9)
xx <- 1:4
wgts <- c(3,2,4,1)
yy2 <- rep(yy,wgts)
xx2 <- rep(xx,wgts)
fit1 <- lm(yy2 ~ xx2)
fit2 <- lm(yy ~ xx, weights = wgts)
fit3 <- lm(yy ~ xx, weights = wgts/mean(wgts))
summary1 <- summary(fit1)
summary2 <- summary(fit2)
summary3 <- summary(fit3)
pred1 <- predict(fit1, list(xx2 = xx), interval = "confidence", se.fit = TRUE)
pred2 <- predict(fit2, list(xx = xx), interval = "confidence", se.fit = TRUE)
pred3 <- predict(fit3, list(xx = xx), interval = "confidence", se.fit = TRUE)

, . :

  • fit2 fit3, , ?
  • (fit2 fit3) ?

. , :

enter image description here

W , . , . , , . sigma2; . , . 2, sigma2 , .

summary2$coef
summary3$coef

#            Estimate Std. Error   t value  Pr(>|t|)
#(Intercept) 2.128713   3.128697 0.6803832 0.5664609
#xx          1.683168   1.246503 1.3503125 0.3094222

pred2
pred3

#$fit
#       fit        lwr      upr
#1 3.811881 -5.0008685 12.62463
#2 5.495050 -0.1299942 11.12009
#3 7.178218  0.6095820 13.74685
#4 8.861386 -1.7302209 19.45299
#
#$se.fit
#       1        2        3        4 
#2.048213 1.307343 1.526648 2.461646 
#
#$df
#[1] 2
#
#$residual.scale  ## for `pred2`
#[1] 3.961448
#
#$residual.scale  ## for `pred3`
#[1] 2.50544

: . . : . , . , , mgcv, .

fit1 fit2? . (n - p). n - , , p - NA, n - p - . p = 2 ( ), fit1 n = 10, fit2 n = 4. , (, ) . .

summary1$coef
#            Estimate Std. Error  t value   Pr(>|t|)
#(Intercept) 2.128713  1.5643486 1.360766 0.21068210
#xx2         1.683168  0.6232514 2.700625 0.02704784

summary2$coef

#            Estimate Std. Error   t value  Pr(>|t|)
#(Intercept) 2.128713   3.128697 0.6803832 0.5664609
#xx          1.683168   1.246503 1.3503125 0.3094222

pred1

#$fit
#       fit      lwr       upr
#1 3.811881 1.450287  6.173475
#2 5.495050 3.987680  7.002419
#3 7.178218 5.417990  8.938446
#4 8.861386 6.023103 11.699669
#
#$se.fit
#        1         2         3         4 
#1.0241066 0.6536716 0.7633240 1.2308229 
#
#$df    # note, this is `10 - 2 = 8`
#[1] 8
#
#$residual.scale
#[1] 1.980724

pred2

#$fit
#       fit        lwr      upr
#1 3.811881 -5.0008685 12.62463
#2 5.495050 -0.1299942 11.12009
#3 7.178218  0.6095820 13.74685
#4 8.861386 -1.7302209 19.45299
#
#$se.fit
#       1        2        3        4 
#2.048213 1.307343 1.526648 2.461646 
#
#$df    # note, this is `4 - 2 = 2`
#[1] 2
#
#$residual.scale  ## for `pred2`
#[1] 3.961448
+1

Source: https://habr.com/ru/post/1658057/


All Articles