smoothfit: Function to produce a smooth curve over a scatterplot

View source: R/smoothfit.r

smoothfitR Documentation

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)

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.

Details

Notice that both variables must be numeric.

Value

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

Author(s)

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.

References

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

Examples

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)

datana documentation built on Aug. 18, 2025, 5:18 p.m.