Tips on using R

Writing text to the clipboard

I just realized that this is really easy as there exists in R function writeClipboard() that copies character strings to the Windows clipboard!

writeClipboard(sprintf("%.2f degrees", 1:10))

For the reverse operation of reading from the clipboard, two functions are available getClipboardFormats() and readClipboard().

Test of significance of linear regression slope

The examples below are for the common cases of a null hypothesis of slope = 0 and slope = 1, but it should be clear how to test for for any arbitrary constant slope value expected a priori of examination of the data. The examples use a one-sided test, in many cases, a two-sided test should be used (e.g. if there is no a priori expectation of the deviation from the null hypothesis to be possible only in one direction). Source unknown, but certainly not of my authorship.

# cars data from R for this example
# linear regression fit
cars.lm <- lm(dist ~ speed, data = cars)
# extract slope estimate and its s.e. indexing by explanatory variable name
my.slope <- summary(cars.lm)$coef["speed", c("Estimate", "Std. Error")]
# extract degrees of freedom
my.df <- summary(cars.lm)$df[2]
# test against hypothesis of no response, i.e. slope = 0
t_value_zero <- my.slope["Estimate"] / my.slope["Std. Error"]
dt(t_value_zero, df = my.df) / 2 # one sided test
# test against hypothesis of 1:1 relationship, i.e. slope = 1
t_value_one <- (my.slope["Estimate"] - 1) / my.slope["Std. Error"]
dt(t_value_one, df = my.df) / 2 # one sided test

Leave a Reply

Your email address will not be published. Required fields are marked *