简体   繁体   English

如何使用R语言从薄板样条(TPS)图中导出数据?

[英]How to export data from a Thin Plate Spline (TPS) plot in R language?

I'm a beginner to R and I am trying to extract data in a gridded format from a Thin Plate Spline plot in the R language. 我是R的初学者,我正尝试从R语言的Thin Plate Spline图中以网格格式提取数据。 Basically I have a data-set of points from across the UK containing the longitude, latitude and amount of rainfall for a particular day. 基本上,我有一个来自英国各地的数据集,其中包含特定日期的经度,纬度和降雨量。 Using the following code I can plot this data onto a UK map: 使用以下代码,我可以将此数据绘制到英国地图上:

dat <- read.table("~jan1.csv", header=T, sep=",")
names(dat) <- c("gauge", "date", "station", "mm", "lat", "lon", "location", "county", "days")
library(fields)
quilt.plot(cbind(dat$lon,dat$lat),dat$mm)
world(add=TRUE)

So far so good. 到现在为止还挺好。 I can also perform a thin plate spline interpolation (TPS) using: 我还可以使用以下方法执行薄板样条插值(TPS):

fit <- Tps(cbind(dat$lon, dat$lat), dat$mm, scale.type="unscaled")

and then I can do a surface plot at a grid scale of my choice eg: 然后我可以按照自己选择的网格比例进行表面绘图,例如:

surface (fit, nx=100, ny=100)

This effectively gives me a gridded data plot at the resolution of 100*100. 这实际上为我提供了分辨率为100 * 100的网格数据图。 So here are my questions: 所以这是我的问题:

  1. How do I extract the data from this gridded data set (ie actual values) to put in a file such as excel or .txt? 如何从该网格化数据集中提取数据(即实际值)以放入excel或.txt等文件中?

  2. How could I change the grid size so the grid starts at a particular x value (and y value) in steps of my choice? 如何更改网格大小,使网格以我选择的步骤从特定的x值(和y值)开始?

With a predict function available, a typical strategy would be to use something like: 在具有predict功能的情况下,典型的策略是使用类似以下内容的方法:

 rnglat <- range(dat$lat)
 rnglon <- range(dat$lon)
 xvals <- seq(rnglon[1], rnglon[2], len=100)
 yvals <- seq(rnglat[1], rnglat[2], len=100)
 griddf <- expand.grid(xvals, yvals)
 griddf$pred <- predict(fit, x=as.matrix(griddf) )

(Since Tps doesn't use a formula interface and predict.Krig doesn't appear to use a newdata argument, I'm not making this in a form that would work for most regression problems.) If you want to narrow the range to something less than the full range or change the number of "grid lines", then modify the seq arguments. (由于Tps并未使用公式接口并进行predict.Krig似乎未使用newdata参数,因此我并不是以适用于大多数回归问题的形式进行此操作。)如果要将范围缩小到小于整个范围的值或更改“网格线”的数量,然后修改seq参数。 (Tested with the fit0 -object constructed in the last example on the fields::predict.Krig help page.) (使用在上一个示例中在fields :: predict.Krig帮助页面上构造的fit0测试。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM