fit 2d surface using LOESS in R -
i want take noisy 2d data , use loess smoothing find smoother 2d surface. data form of data frame or matrix of r rows , c columns. 'asbio' package surface plots n*n data, , can work out how 1d fits 'loess' stats base package can't work out how 2d plot.
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 colours 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 heights each point. surface3d(age,year,as.matrix(z), col=col) loess(z~age+year,data=data.frame(z))
i error rejecting data.frame being list, suspect there more that. searching information, can find 1d linear descriptions.
can help?
worked out....
the data frame or matrix needs converted 3 columns data frame. once data frame z above created, following smooth , display (using rgl).
open3d(usefreetype=par3d("usefreetype")) surface3d(age,year,as.matrix(z), col=col) zz <- z zz$ages <- rownames(z) zzx <- melt(zz, id.vars="ages", value.name="vals") theresult <- loess(vals~as.numeric(ages)+as.numeric(variable),data=data.frame(zzz)) resulttable <- matrix(theresult$fitted, nrow=length(age), length(year)) open3d(usefreetype=par3d("usefreetype")) surface3d(age,year,as.matrix(resulttable), col=col)
Comments
Post a Comment