我希望使用LOESS平滑法处理嘈杂的2D数据,以找到更平滑的2D曲面。该数据以数据框或r行c列的矩阵形式存在。'asbio'包可以绘制n*n数据的曲面图,而我可以使用统计基础包中的'loess'进行1D拟合,但我无法弄清如何进行2D绘图。
library(rgl)
age <- 1:100
year <- 1959:2012
z<- data.frame(matrix(array(0, length(age)*length(year)),nrow=length(age), ncol=length(year)))
rownames(z) <- age
colnames(z) <- year
distYear <- dnorm(1:length(year), 26, 10)
distAge <- dnorm(age, 50, 15)
for(theYear in year){
#for(theAge in age){
z[,theYear-year[1]+1] <- as.numeric(distYear[theYear-year[1]+1]*distAge*runif(length(age))*50000)
#}
}
z<- data.matrix(z)
z <- data.frame(z)
colnames(z) <- year
# First define the colours for the Z values.
zlim <- range(z)
zlen <- zlim[2] - zlim[1]
scale <- 20/zlen
colorlut <- terrain.colors(20,alpha=1.0) # height color lookup table
col <- colorlut[ (t(as.matrix(z))-zlim[1])*scale+1 ] # assign colors to heights for each point.
surface3d(age,year,as.matrix(z), col=col)
loess(z~age+year,data=data.frame(z))
我收到一个错误,拒绝了data.frame作为列表,但我怀疑问题不止于此。在寻找信息时,我只能找到一维线性描述。
有人可以帮忙吗?