这个r语言代码在干什么:好的,那这个代码在干嘛:install.packages("fda") library(fda) tobs = seq(0,1,0.01) nobs =…
- User
这个r语言代码在干什么:好的,那这个代码在干嘛:install.packages("fda") library(fda)
tobs = seq(0,1,0.01) nobs = length(tobs) knots = c(seq(0,1,0.1)); nknots = length(knots); norder = 4; nbasis = length(knots) + norder - 2; basis = create.bspline.basis(c(min(tobs),max(tobs)),nbasis,norder,knots);
basis values at samplincurv points
basismat = eval.basis(tobs, basis); dim(basismat) quartz() plot(tobs,basismat[,1],type = "l",col=1,lwd=3) lines(tobs,basismat[,2],type = "l",col=2,lwd=3)
quartz() matplot(tobs,basismat,type='l',lwd=2,lty=1, xlab='day',ylab='basis',cex.lab=1.5,cex.axis=1.5) for (i in 1:nknots) { abline(v=knots[i],type="l", lty=2, lwd=3) }
Comments: If x(0) = 1, set the coefficient to the first basis function = 1;
evaluate the first derivative of the basis functions
Dbasismat = eval.basis(tobs, basis,1); quartz() matplot(tobs,Dbasismat,type='l',lwd=2,lty=1, xlab='day',ylab='basis',cex.lab=1.5,cex.axis=1.5) for (i in 1:nknots) { abline(v=knots[i],type="l", lty=2, lwd=3) }
true curve
ytru = (tobs-0.3)^2 plot(tobs,ytru,type = "l")
put noise to the true curve and generate noisy data
nobs = length(tobs) noise = 0.03*rnorm(nobs) yobs = ytru + noise points(tobs,yobs)
estimate basis coefficient
Mmat = ginv(t(basismat)%%basismat)%%t(basismat) chat = Mmat%*%yobs
fitted curve
yhat = basismat%*%chat; lines(tobs,yhat,type = "l",col="red")
estimate the variance of noise
SSE = t(yhat-yobs)%*%(yhat-yobs) sigma2 = SSE/(nobs-nbasis) sigma2 sqrt(sigma2)
estimate the variance of the fitted curve
Smat = basismat%%Mmat varYhat = diag(Smat%%Smat*matrix(sigma2,nobs,nobs))
95% confidence interval
yhat025 = yhat-1.96sqrt(varYhat) yhat975 = yhat+1.96sqrt(varYhat)
lines(tobs,yhat025,type="l", lty=2, lwd=3,col="blue") lines(tobs,yhat975,type="l", lty=2, lwd=3,col="blue")
###########################
Smoothing Splines
#########################
Use quadrature to get integral - Composite Simpson's Rule
delta <- 0.02 quadpts <- seq(0,1,delta) nquadpts <- length(quadpts) quadwts <- as.vector(c(1,rep(c(4,2),(nquadpts-2)/2),4,1),mode="any") quadwts <- c(1,rep(c(4,2),(nquadpts-1)/2)) quadwts[nquadpts] <- 1 quadwts <- quadwts*delta/3
Second derivative of basis functions at quadrature points
Q2basismat = eval.basis(quadpts, basis,2);
estimates for basis coefficients
Rmat = t(Q2basismat)%%(Q2basismat(quadwts%*%t(rep(1,nbasis))))
dim(Rmat) basismat2 = t(basismat)%%basismat; lambda = 0.05 # smoothing parameter Bmat = basismat2 + lambdaRmat; chat = ginv(Bmat)%%yobs;
- Assistant
- User
- Assistant
这两段代码都使用了R语言中的函数型数据分析(FDA)工具来进行数据的平滑和拟合。它们的工作中使用了fda包和B样条基函数。尽管它们的目标和使用的数据有所不同,但基本的分析方法却有相似之处。下面我们分别讨论它们的相似点和不同点。
相似之处
-
使用同样的工具包:
- 两个代码都使用了
fda包来创建和评估样条基函数。
- 两个代码都使用了
-
样条基函数的生成:
- 两者都创建了 B 样条基函数,并使用
create.bspline.basis函数。 - 两者都考虑了基函数的阶数(
norder)和节点(knots)。
- 两者都创建了 B 样条基函数,并使用
-
平滑和拟合:
- 两者都进行了数据平滑。第一个代码使用了最小二乘法来估计样条系数,而第二个代码通过调试基函数数量和惩罚参数(lambda)进行平滑。
- 两者都评估了平滑曲线并进行绘图。
-
误差评估:
- 两者都计算了某种形式的误差来评估模型的拟合度。第一个代码计算了残差平方和(SSE)和残差方差,第二个代码通过最小化广义交叉验证(GCV)来选择最佳的lambda。
不同之处
-
数据处理:
- 第一个代码是为了生成和处理合成数据,主要目标是演示样条函数的拟合和评估。
- 第二个代码处理的是实际生育率数据,从文件中读取并拟合这些数据。
-
目标不同:
-