library(ggplot2)
library(sp)
sdf <- SpatialPointsDataFrame(df3[,c("lon","lat")],df3)
sdf <- SpatialPointsDataFrame(df3[,c("lon","lat")],df3,proj4string = CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"))

plot(sdf)
sdf$m3ar <- sdf$ar/sdf$ater

library(GWmodel)

hist(sdf$m3ar)
hist(log(sdf$m3ar))
quick.map <- function(spdf,var,legend.title,main.title)
{
	x <- spdf@data[,var]
	cut.vals <- pretty(x,5)
	x.cut <- cut(x,cut.vals)
	cut.levels <- levels(x.cut)
	cut.band <- match(x.cut,cut.levels)
	colors <- brewer.pal(length(cut.levels), "YlOrRd")
	colors <- rev(colors)
	par(mar=c(1,1,1,1))
	plot(spdf,col=colors[cut.band],pch=16)
	title(main.title)
	legend("topleft",cut.levels,col=colors,pch=16,bty="n",title=legend.title)
}

localstat1 <- gwss(sdf,var=c("ar","ater","m3ar"),bw=0.3,quantile=T)
locstat <- bw.gwss.average(sdf,vars=c("ar","ater","m3ar"))
locstat
localstat1

library(RColorBrewer)
localstat1$SDF$m3
quick.map(localstat1$SDF, "m3ar_Median","tut","tit")

colnames(sdf)
grd <- SpatialGrid(GridTopology(c(18.93,47.39),c(.005,.005),c(80,50)))
grd2 <- c(grd,grd)
grd2
plot(grd2)
DM <- gw.dist(dp.locat=coordinates(sdf),rp.locat = coordinates(grd))
DM2 <- gw.dist(dp.locat=coordinates(sdf2),rp.locat = coordinates(grd))
sdf2 <- sdf[!is.na(sdf$Emelet),]
regrs <- gwr.basic(log(m3ar)~log(ater),sdf,bw=0.02,kernel = "exponential", dMat = DM, regression.points = grd)
regrs <- gwr.basic(log(m3ar)~log(ater)+log(tomkoz+1),sdf,bw=0.02,kernel = "exponential", dMat = DM, regression.points = grd)
regrs <- gwr.basic(log(m3ar)~log(tomkoz+1)+as.factor(Emelet)+as.factor(ker),sdf2,bw=0.30,kernel = "exponential", dMat = DM2, regression.points = grd)
table(sdf$Emelet)
(regrs)
regrs$SDF$as.factor.ker.2
image(regrs$SDF,'log.tomkoz...1.') #,xlim=c(19.135,19.139),ylim=c(47.45,47.55))
image(regrs$SDF,'as.factor.ker.10') #,xlim=c(19.135,19.139),ylim=c(47.45,47.55))
contour(regrs$SDF,'log.tomkoz...1.',add=T)
plot(sdf, add=T, col="blue",alpha=0.1)

plot(grd)
