| Line {pcds} | R Documentation |
The line joining two distinct 2D points
a and b
Description
An object of class "Lines".
Returns the equation, slope, intercept,
and y-coordinates of the line crossing two
distinct 2D points a and b
with x-coordinates provided in vector x.
This function is different from the line function
in the standard stats package
in R in the sense that Line(a,b,x) fits the line passing
through points a and b
and returns various quantities (see below) for this line and x is
the x-coordinates of the points
we want to find on the Line(a,b,x)
while line(a,b) fits the line robustly
whose x-coordinates are in a
and y-coordinates are in b.
Line(a,b,x) and line(x,Line(A,B,x)$y)
would yield the same straight line
(i.e., the line with the same coefficients.)
Usage
Line(a, b, x)
Arguments
a, b |
2D points that determine the straight line (i.e., through which the straight line passes). |
x |
A scalar or a |
Value
A list with the elements
desc |
A description of the line |
mtitle |
The |
points |
The input points |
x |
The input scalar or |
y |
The output scalar or |
slope |
Slope of the line, |
intercept |
Intercept of the line
passing through points |
equation |
Equation of the line
passing through points |
Author(s)
Elvan Ceyhan
See Also
slope, paraline,
perpline, line
in the generic stats package and and Line3D
Examples
A<-c(-1.22,-2.33); B<-c(2.55,3.75)
xr<-range(A,B);
xf<-(xr[2]-xr[1])*.1
#how far to go at the lower and upper ends in the x-coordinate
x<-seq(xr[1]-xf,xr[2]+xf,l=5) #try also l=10, 20, or 100
lnAB<-Line(A,B,x)
lnAB
summary(lnAB)
plot(lnAB)
line(A,B)
#this takes vector A as the x points and vector B as the y points and fits the line
#for example, try
x=runif(100); y=x+(runif(100,-.05,.05))
plot(x,y)
line(x,y)
x<-lnAB$x
y<-lnAB$y
Xlim<-range(x,A,B)
if (!is.na(y[1])) {Ylim<-range(y,A,B)} else {Ylim<-range(A,B)}
xd<-Xlim[2]-Xlim[1]
yd<-Ylim[2]-Ylim[1]
pf<-c(xd,-yd)*.025
#plot of the line joining A and B
plot(rbind(A,B),pch=1,xlab="x",ylab="y",
xlim=Xlim+xd*c(-.05,.05),ylim=Ylim+yd*c(-.05,.05))
if (!is.na(y[1])) {lines(x,y,lty=1)} else {abline(v=A[1])}
text(rbind(A+pf,B+pf),c("A","B"))
int<-round(lnAB$intercep,2) #intercept
sl<-round(lnAB$slope,2) #slope
text(rbind((A+B)/2+pf*3),ifelse(is.na(int),paste("x=",A[1]),
ifelse(sl==0,paste("y=",int),
ifelse(sl==1,ifelse(sign(int)<0,paste("y=x",int),paste("y=x+",int)),
ifelse(sign(int)<0,paste("y=",sl,"x",int),paste("y=",sl,"x+",int))))))