# Fonction de répartition empirique

Ce matin, en cours, on avait utilisé une série d’observations pour décrire l’estimation nonparamétrique de la fonction de répartition

> Davis=read.table(
"http://socserv.socsci.mcmaster.ca/jfox/Books/Applied-Regression-2E/datasets/Davis.txt")

Comme il y une faute de saisie dans la base, on corrige, à la main,

> Davis[12,c(2,3)]=Davis[12,c(3,2)]

Notre variable d’intérêt sera la taille d’un individu,

> X=Davis$height > X[1:10] [1] 182 161 161 177 157 170 167 186 178 171 Pour tracer la fonction de répartition empirique, $\widehat{F}_n(x)=\frac{1}{n}\sum_{i=1}^n\boldsymbol{1}(\{x_i\leq x\})$ on peut utiliser le code suivant > n=length(X) > x=seq(140,200) > ggplot(data=data.frame(x=x), aes(x = x))+ + stat_function(fun=Vectorize(function(x) sum(X<=x)/n), + geom="step", col = "red", size = 1) + + ylab("Fonction de Repartition") # Moving the North Pole to the Equator I am still working with @3wen on visualizations of the North Pole. So far, it was not that difficult to generate maps, but we started to have problems with the ice region in the Arctic. More precisely, it was complicated to compute the area of this region (even if we can easily get a shapefile). Consider the globe, worldmap <- ggplot() + geom_polygon(data = world.df, aes(x = long, y = lat, group = group)) + scale_y_continuous(breaks = (-2:2) * 30) + scale_x_continuous(breaks = (-4:4) * 45) and then, add three points in the northern hemisphere, and plot the associated triangle P1 <- worldmap + geom_polygon(data = triangle, aes(x = long, y = lat, group = group), fill ="blue", alpha = 0.6, col = "light blue", size = .8)+ geom_point(data = triangle, aes(x = long, y = lat, group = group),colour = "red")+ for some given projection, e.g. coord_map("ortho", orientation=c(61, -74, 0)) This can be done with the following function proj1=function(x=75){ triangle <- data.frame(long=c(-70,-110,-90*(x<90)+90*(x>90)), lat=c(60,60,x*(x<90)+(90-(x-90))*(x>90)),group=1, region=1) worldmap <- ggplot() + geom_polygon(data = world.df, aes(x = long, y = lat, group = group)) + scale_y_continuous(breaks = (-2:2) * 30) + scale_x_continuous(breaks = (-4:4) * 45) P1 <- worldmap + geom_polygon(data = triangle, aes(x = long, y = lat, group = group), fill ="blue", alpha = 0.6, col = "light blue", size = .8)+ geom_point(data = triangle, aes(x = long, y = lat, group = group),colour = "red")+ coord_map("ortho", orientation=c(61, -74, 0)) print(P1) } or I am not sure if I understand why the projection of the triangle is not convex on the graph above, but say it’s not a big deal, here. Actually, our problem is that our interest is on regions (polygons, from a geometrical point of view) that do contain the North Pole. And here, it starts to be messy. I can easily move the upper point on the other side of the globe, but the polygon is not correct, I do understand that it should be a problem, non-trivial, but it means that it should not be that simple to compute the area of a polygon (a region) that contains the North Pole. Which is exactly what we did observe in our computation. And I believe that one heuristic interpretation is related to the following graph My skills in geometry are extremely poor. So do not expect that I will go through the code of the function that compute the area of a polygon ! Actually, my idea is the following : if the problem is that the North Pole is in the region, let’s consider some rotation, to shift the North Pole on the Equation. The code here is, from latitudes and longitude, to get new latitudes and longitudes, after a rotation around the y-axis (the North Pole will go down, along Greenwhich meridian) is rotation=function(Z,theta){ lon=Z[,1]/180*pi; lat=Z[,2]/180*pi x=cos(lon)*cos(lat) y=sin(lon)*cos(lat) z=sin(lat) pt1=cbind(x,y,z) M=matrix(c(cos(theta),0,-sin(theta),0,1,0,sin(theta),0,cos(theta)),3,3) pt2=t(M%*%t(pt1)) lat=asin(pt2[,3])*180/pi lon=atan2(pt2[,2],pt2[,1])*180/pi return(cbind(lon,lat))} With a rotation from $0$ (no change) to $\pi/2$ (the North Pole on the equator), we get From now on, it is possible to compute the area of any region containing the North Pole ! One should simply apply the rotation function on all datebases generated from shapefiles (and then the opposite rotation to get a proper location) ! We can then compute the centroid of the ice region, for example, r.glace=glace r.glace[,1:2]=rotation(glace[,1:2],pi/2) M=matrix(NA,length(unique(glace$id)),3)
j=0
for(i in unique(glace$id)){j=j+1 Polyglace <- as(r.glace[glace$id==i,c("long","lat")],"gpc.poly")
M[j,1]=area.poly(Polyglace)
M[j,2:3]=centroid(r.glace[r.glace\$id==i,c("long","lat")])
}
Z=c(weighted.mean(M[,2],M[,1]),weighted.mean(M[,3],M[,1]))
rotation(rbind(Z),-pi/2)[1,])

And we get

and below, we can visualize all the locations of the centroid of the ice region in the past 25 years