报错: quartz() Error in quartz() : 没有"quartz"这个函数
- User
报错: quartz() Error in quartz() : 没有"quartz"这个函数
- Assistant
quartz()函数是 R 编程语言中用于在 macOS 系统上打开图形设备的函数。如果你在其他操作系统(如 Windows 或 Linux)上尝试调用quartz(),R 会报错,因为这个函数在这些操作系统上不可用。如果你在 Windows 或 Linux 系统上运行 R,可以使用其他绘图设备。例如:
- 在 Windows 上,你可以使用
windows():windows() plot(1:10, 1:10) - 在 Linux 上,你可以使用
x11():x11() plot(1:10, 1:10)
此外,
plot()函数通常会自动选择合适的图形设备,你不一定需要手动调用图形设备函数。如果你希望创建一个平台无关的解决方案,可以考虑使用函数
pdf()或png(),直接将图形输出到文件中:pdf("output.pdf") plot(1:10, 1:10) dev.off()png("output.png") plot(1:10, 1:10) dev.off()总结:根据你所在的操作系统选择合适的图形设备函数,或者将图形输出到文件中,以便在不同平台上使用。
- 在 Windows 上,你可以使用
- User
这个代码在干嘛# R codes for Estimating Constraint functions
library('fda')
Example when estiamting a function with the positive constraint.
Vancouver precipitation data
VancPrec = CanadianWeather$dailyAv[,'Vancouver','Precipitation.mm']
length(VancPrec) day = 1:365
weather data
fbasis = create.fourier.basis(c(1,365),365)
define the harmonic acceleration differential operator
L = D^3 f(t) + 0 * D^2 f(t) + w^2 * D^f(t) + 0 * f(t)
It will not penalize a+bt, cos(wt) and sin(wt)
w = 2pi/period of the process
c(0,(2*pi/365)^2,0) is the vector of coefficients to
the lower derivatives
harmLfd = vec2Lfd(c(0,(2*pi/365)^2,0), c(1, 365))
Tell 3 information
1:fbasis - Type of basis functions
2: harmLfd - Define the roughness penalty
3: lambda - value of the smoothing parameter
harmpar = fdPar(fbasis,harmLfd,lambda=1e4)
smoothing without considering the positive contraint;
precfd1 = smooth.basis(day,VancPrec,harmpar)
smoothing when considering the positive contraint;
posprecfd1 = smooth.pos(day,VancPrec,harmpar)
plot the fitte curves
plot(1:10, 1:10) par(mfrow=c(1,1),mar = c(8, 8, 4, 2)) plot(day,VancPrec,col=2,xlab='day',ylab='precipitation',cex.lab=1.5,cex.axis=1.5)
plot the estimated function without considering the positive constraints
lines(precfd1$fd,col=4,lwd=2)
plot the estimated function when considering the positive constraints
posvals = eval.posfd(day,posprecfd1$Wfdobj) lines(day,posvals,col="black",lwd=2)
fit data with the smoothing parameter lambda = 10^2
posharmpar = fdPar(fbasis,harmLfd,lambda=1e2) posprecfd2 = smooth.pos(day,VancPrec,posharmpar)
posvals = eval.posfd(day,posprecfd2$Wfdobj) lines(day,posvals,col=6,lwd=2)
legend(0,8.5,legend=c('fd lambda=1e4','pos.fd lambda=1e4','pos.fd lambda=1e2'),col=c(4,"black",6),lwd=2)
Example when estiamting a function with the monotone constraint.
Now the growth data
names(growth) growth
hgtm = growthhgtm # heights of boys hgtf = growthhgtf # heights of girls age = growth$age # age of measurements
knots <- growth$age norder <- 5 # because the transformation is equivalent to estiamting the derivative.
So here we increase the order of B-splines by 1.
nbasis <- length(knots) + norder - 2 rng <- c(min(growthage)) hgtbasis <- create.bspline.basis(rng, nbasis, norder, knots)
define the roughness penalty
Lfdobj <- 2 # use the second derivative to define the roughness penalty lambda <- 1e-2 growfdPar <- fdPar(hgtbasis, Lfdobj, lambda)
estimate the function without considering the monotone contraint
Smoothing for all 39 male subjects
Each male subject has his data stored in columns of "hgtm"
hgtmfd <- smooth.basis(age, hgtm, growfdPar)$fd dim(hgtm) plot(1:10, 1:10) par(mfrow=c(1,1),mar = c(8, 8, 4, 2)) plot(age,hgtm[,25],xlab='age',ylab='height',col=2,cex.lab=1.5,cex.axis=1.5) lines(hgtmfd[25],col=4,lwd=2) # estimated function without considering the monotone contraint
estimate the function when considering the monotone contraint
monhtmfd25 = smooth.monotone(age,hgtm[,25],growfdPar) Wfdobj = monhtmfd25beta
tfine = seq(1,18,len=101) hgt25vals = eval.monfd(tfine,Wfdobj)
plot the function when considering the monotone contraint
lines(tfine,beta[1] + beta[2]*hgt25vals,col=6,lwd=2)
plot the derivative of the estimated function
plot(1:10, 1:10) par(mfrow=c(1,1),mar = c(8, 8, 4, 2)) plot(hgtmfd[25],Lfdobj=1,xlab='age',ylab='Dheight',col=4,cex.lab=1.5,cex.axis=1.5,lwd=2)
Dhgt25vals = eval.monfd(tfine,Wfdobj,Lfdobj=1) lines(tfine,beta[2]*Dhgt25vals,col=6,lwd=2)
Example to estimate Probability density Function
simulate some data based on the normal distribution
set up range for density
rangeval <- c(-3,3)
set up some standard normal data
x <- rnorm(50)
make sure values within the range
x[x < -3] <- -2.99 x[x > 3] <- 2.99
plot the histogram of x
plot(1:10, 1:10) par(mfrow=c(1,1),mar = c(8, 8, 4, 2)) hist(x)
set up basis for W(x)
We choose 11 basis functions
basisobj <- create.bspline.basis(rangeval, 11,norder=6)
define the roughness penalty on W(x)
set up initial value for Wfdobj
W(x) = 0
Wfd0 <- fd(matrix(0,11,1), basisobj) Wlambda = 1e-2 WfdParobj <- fdPar(Wfd0, 3, Wlambda)
estimate density
denslist <- density.fd(x, WfdParobj)
plot density
xval <- seq(-3,3,.2) wval <- eval.fd(xval, denslistC
plot(1:10, 1:10) par(mfrow=c(1,1),mar = c(8, 8, 4, 2)) plot(xval, pval, type="l", ylim=c(0,1),lwd=3) points(x,rep(0,50))
- Assistant
这个 R 代码示例展示了如何在光滑曲线估计中应用约束条件。代码展示了如何使用约束平滑函数来满足特定条件,例如非负性(positive constraint)、单调性(monotone constraint)以及估计概率密度函数(PDF)。以下是代码的详细解析:
1. 准备工作
library('fda')加载
fda包,它提供了在功能数据分析中使用的各种工具。2. 非负约束的光滑估计(Vancouver降水数据)
数据和基础设置
VancPrec = CanadianWeather$dailyAv[,'Vancouver','Precipitation.mm'] # 加载温哥华的降水数据 day = 1:365 # 一年的天数 fbasis = create.fourier.basis(c(1,365),365) # 创建傅里叶基函数定义谐波加速微分算子和光滑参数
harmLfd = vec2Lfd(c(0,(2*pi/365)^2,0), c(1, 365)) harmpar = fdPar(fbasis,harmLfd,lambda=1e4)定义一个谐波加速微分算子,以确保光滑曲线不会对简单线性函数和二次函数进行惩罚。
应用非负约束
precfd1 = smooth.basis(day,VancPrec,harmpar) posprecfd1 = smooth.pos(day,VancPrec,harmpar)分别使用无约束和非负约束进行光滑曲线估计。
绘图
plot(day,VancPrec,col=2,xlab='day',ylab='precipitation',cex.lab=1.5,cex.axis=1.5) lines(precfd1$fd,col=4,lwd=2) posvals = eval.posfd(day,posprecfd1$Wfdobj) lines(day,posvals,col="black",lwd=2)绘制无约束和非负约束下的光滑曲线。
3. 单调约束的光滑估计(身高增长数据)
数据和基础设置
hgtm = growth$hgtm # 男孩的身高数据 age = growth$age # 测量年龄 knots <- growth$age # 结点 norder <- 5 # B-样条函数的阶数 nbasis <- length(knots) + norder - 2 # 基函数的数量 hgtbasis <- create.bspline.basis(range(growth$age), nbasis, norder, knots)定义粗糙度惩罚参数
Lfdobj <- 2 # 二阶导数的粗糙度惩罚 lambda <- 1e-2 growfdPar <- fdPar(hgtbasis, Lfdobj, lambda)光滑曲线估计
hgtmfd <- smooth.basis(age, hgtm, growfdPar)$fd应用单调约束
monhtmfd25 = smooth.monotone(age,hgtm[,25],growfdPar) Wfdobj = monhtmfd25$Wfdobj beta = monhtmfd25$beta绘图和导数估计
plot(age,hgtm[,25],xlab='age',ylab='height',col=2,cex.lab=1.5,cex.axis=1.5) lines(hgtmfd[25],col=4,lwd=2) tfine = seq(1,18,len=101) hgt25vals = eval.monfd(tfine,Wfdobj) lines(tfine,beta[1] + beta[2]*hgt25vals,col=6,lwd=2) plot(hgtmfd[25],Lfdobj=1,xlab='age',ylab='Dheight',col=4,cex.lab=1.5,cex.axis=1.5,lwd=2) Dhgt25vals = eval.monfd(tfine,Wfdobj,Lfdobj=1) lines(tfine,beta[2]*Dhgt25vals,col=6,lwd=2)绘制原始数据、光滑曲线和单调约束下的曲线。
4. 估计概率密度函数
模拟数据和基础设置
rangeval <- c(-3,3) x <- rnorm(50) x[x < -3] <- -2.99 x[x > 3] <- 2.99 hist(x) # 绘制直方图生成和处理正态分布模拟数据。
基函数和粗糙度惩罚
basisobj <- create.bspline.basis(rangeval, 11, norder=6) Wfd0 <- fd(matrix(0,11,1), basisobj) Wlambda = 1e-2 WfdParobj <- fdPar(Wfd0, 3, Wlambda)密度估计
denslist <- density.fd(x, WfdParobj)绘图
xval <- seq(-3,3,.2) wval <- eval.fd(xval, denslist$Wfdobj) pval <- exp(wval) / denslist$C plot(xval, pval, type="l", ylim=c(0,1), lwd=3) points(x, rep(0,50))估计并绘制概率密度函数。
总结
上述代码段使用了
fda包进行功能数据分析,展示了如何在光滑曲线估计中应用约束条件,例如非负约束和单调约束,以及如何估计概率密度函数。这些技术对于处理实际数据中需要遵循某些物理或数学约束的情况非常有用。