knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
See Code Box 2.1, $P$-value is about 0.008. This means that the test statistic is unusually small so there is good evidence of an effect of nicotine. More directly, starting from the stated test statistic of $-2.67$, the test statistic has a $t_{n_1+n_2-2}$ distribution under the hypothesis of no effect, so we could calculate the $P$-value directly as:
pt(-2.67,10+10-2)
library(ecostats) data(guineapig) t.test(errors~treatment, data=guineapig, var.equal=TRUE, alternative="less")
The normal quantile plots of Figure 2.1 were generated using the below code.
qqenvelope(guineapig$errors[guineapig$treatment=="N"]) qqenvelope(guineapig$errors[guineapig$treatment=="C"]) by(guineapig$errors,guineapig$treatment,sd)
While all the points lie in the simulation envelope, there is a clear curve on both of them suggesting some right skew. Also, the standard deviations are quite different, not quite a factor of two, but getting close. So it might be worth looking at (log-)transformation.
The research question is how it [IBI] related to catchment area which is an estimation question, we want to estimate the relationship between IBI and catchment area.
There are two variables:
I would use a scatterplot:
data(waterQuality) plot(quality~logCatchment, data=waterQuality)
And I would fit a linear regression model, as in Code Box 2.3.
data(waterQuality) fit_qual=lm(quality~logCatchment, data = waterQuality) summary(fit_qual)
An approximate 95% CI is
-11.042+c(-2,2)*1.780
or you could use confint
:
confint(fit_qual)
Either way we are 95% confident that as logCatchment changes by 1 (meaning a ten-fold increase in catchment area, since a log10-transformed of catchment area was used), IBI decreases by between about 7 and 15.
To produce a residual vs fits plot, and a normal quantile plot of residuals, you just take a fitted regression object (like fit_qual
, produced in Code Box 2.3) and apply the plot function:
plot(fit_qual, which=1:2)
The which argument lets you choose which plot to construct (1=residuals vs fits, 2=normal quantile plot).
Alternatively, we can use ecostats::plotenvelope
to add simulation envelopes around points on these plots, to check if any deviations from expected patterns are large compared
to what we might expect for datasets that satisfy model assumptions:
library(ecostats) plotenvelope(fit_qual, which=1:2)
Assumptions look reasonable here -- there is no trend in the residual vs fits plot, and the normal quantile plot is close to a straight line. Points are all well within their simulation envelopes, suggesting that departures are also small compared to what would be expected if the model were correct.
There are four regression assumptions:
t.test(errors~treatment, data=guineapig, var.equal=TRUE)
ft_guineapig=lm(errors~treatment,data=guineapig) summary(ft_guineapig)
Angela has two variables of interest -- height and latitude -- and both are quantitative. So linear regression is a good starting point.
library(ecostats) data(globalPlants) plot(height~lat,data=globalPlants) ft_height=lm(height~lat,data=globalPlants) summary(ft_height) plotenvelope(ft_height,which=1:2)
The original scatterplot suggests data are "pushed up" against the boundary of height=0, suggesting log-transformation. The linear model residual plots substantiate this, with the normal quantile plot clearly being strongly right-skewed, and the residual vs fits plot suggesting a fan-shape.
So let's log-transform height.
plot(height~lat,data=globalPlants,log="y") globalPlants$logHeight=log10(globalPlants$height) ft_logHeight=lm(logHeight~lat,data=globalPlants) summary(ft_logHeight) plotenvelope(ft_logHeight,which=1:2)
OK suddenly everything is looking a lot better!
It will actually be slightly easier to check assumptions using a linear model, so I'll use lm
for reanalysis.
data(guineapig) guineapig$logErrors=log(guineapig$errors) ft_logGuineapigs = lm(logErrors~treatment, data=guineapig) summary(ft_logGuineapigs) plotenvelope(ft_logGuineapigs) by(guineapig$logErrors,guineapig$treatment,sd)
We are doing much better with assumptions: plots look better, standard deviations are more similar. Results became slightly more significant, which is not unexpected, as data are closer to normal (which usually means that tests based on linear models have more power).
Notice that there is no noticeable smoother or envelope on the residual vs fits plot -- that is because for a t-test (and later on, one-way or factorial ANOVA designs) the mean of the residuals is exactly zero for all fitted values. Trying to assess the trend on this plot for non-linearity is not really useful, the only thing to worry about here is a fan shape. This would also show up on a smoother through the scale-location plot as an increasing trend:
plotenvelope(ft_logGuineapigs,which=3)
but there is no increasing trend so we are all good :)
data(waterQuality) ft_water = lm(quality~logCatchment,data=waterQuality) summary(ft_water) ft_water2 = lm(quality~logCatchment,data=waterQuality, subset=waterQuality$logCatchment>2) summary(ft_water2)
The $R^2$ value decreased a fair bit, which makes a bit of sense because we have removed from the dataset a point which has an extreme $X$ value. $R^2$ is a function of the sampling design and sampling a broader range of catchment areas would increase $R^2$, and we have just decreased the range of catchment areas being included in analysis.
The $P$-value decreased slightly for similar reasons.
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.