线没有进入散点图 R

Line not getting in scatterplot R

我在 R 中获取一行时遇到问题。我有以下代码:

#7.4
NFull <- tp$ntest;
Ni <- 0.7*log(tp$ntest);
#install.packages(mgcv)
library(mgcv)
plot(tp$pos ~ tp$dateno, main="Deltaudglatning")
xval <- with(tp, seq(min(tp$dateno), max(tp$dateno), length.out = 224))


fitgam <- gam(tp$pos ~ s(tp$dateno, k=4)+offset(Ni), tp, family=quasipoisson, method="REML")
summary(fitgam)
lines(xval, predict(fitgam, data.frame(xval),type="response"), col="green")

我想在这个散点图中得到它: [![在此处输入图片描述][1]][1] 有人可以帮忙吗?

我的数据截图,共224行: [![在此处输入图片描述][2]][2]

Link 到数据: [1]: https://i.stack.imgur.com/DoOKR.png [2]: https://i.stack.imgur.com/KFycU.png

您的问题是您没有在提供预测函数的数据框中命名您尝试预测的变量。

这是一个应该有效的模拟数据示例(显然完全不同):

tp <- data.frame(date = as.Date('2020-04-01') + 0:223,
                 dateno = 1:224,
                 ntest = sample(1000:7000, 224),
                 pos = sample(140:500,224,T))

NFull <- tp$ntest;
Ni <- 0.7*log(tp$ntest);
#install.packages(mgcv)
library(mgcv)
#> Loading required package: nlme
#> This is mgcv 1.8-33. For overview type 'help("mgcv-package")'.
plot(tp$pos ~ tp$dateno, main="Deltaudglatning")
xval <- with(tp, seq(min(tp$dateno), max(tp$dateno), length.out = 224))


fitgam <- gam(pos ~ s(dateno, k=4)+offset(Ni), tp, family=quasipoisson, method="REML")
summary(fitgam)
#> 
#> Family: quasipoisson 
#> Link function: log 
#> 
#> Formula:
#> pos ~ s(dateno, k = 4) + offset(Ni)
#> 
#> Parametric coefficients:
#>              Estimate Std. Error t value Pr(>|t|)
#> (Intercept) -0.008858   0.033060  -0.268    0.789
#> 
#> Approximate significance of smooth terms:
#>           edf Ref.df     F p-value
#> s(dateno)   1      1 1.422   0.234
#> 
#> R-sq.(adj) =  -1.03   Deviance explained = 0.629%
#> -REML = 607.37  Scale est. = 79.403    n = 224
lines(xval, predict(fitgam, data.frame(dateno = xval),type="response"), col="green")
#> Warning in predict.gam(fitgam, data.frame(dateno = xval), type = "response"): not all required variables have been supplied in  newdata!

reprex package (v0.3.0)

于 2020-12-17 创建