Learn R Programming

datana (version 1.1.2)

smoothfit: Function to produce a smooth curve over a scatterplot

Description

The function estimates a simple locally weighted regression. The main aim of the function is to be used to describe a graphical pattern between two variables in a scatterplot.

Usage

smoothfit(x = x, y = y, linecol = "blue", lty = 2, lwd = 1)

Value

The function returns the above described curve, but in order to be used, you must first to create the scatterplot.

Arguments

x

A numeric vector representing the X-axis variable.

y

A numeric vector representing the Y-axis variable (response).

linecol

A string specifying the smooth line color. The default is set to "blue".

lty

The type of line to be draw according to the R plot parameters. By default is set to 2 which is a dashed line.

lwd

The wide of the line to be draw according to the R plot parameters. By default is set to 1.

Author

A somehow related version of this function was first created by Prof. Timothy Gregoire (Yale University), but the current version is due to Christian Salas-Eljatib.

Details

Notice that both variables must be numeric.

References

  • Weisberg S. Applied Linear Regression. 3rd edition. Wiley, New York, NY, USA. 310 p.

Examples

Run this code
df <- datana::annualppCities
# Example 1
plot(annual.pp~year, data=df, col="gray")
smoothfit(x=df$year, y=df$annual.pp)
# Example 2
df2<-subset(df,city=="Chillan")
plot(annual.pp~year, data=df2, col="gray")
smoothfit(x=df2$year, y=df2$annual.pp, linecol="red",lwd=3)

Run the code above in your browser using DataLab