Title: | Knock Errors Off Nice Guesses |
---|---|
Description: | Miscellaneous functions and data used in Qingyao's psychological research and teaching. Keng currently has a built-in dataset depress, and could (1) scale a vector, (2) test the significance and compute the cut-off values of Pearson's r without raw data, (3) compare lm()'s fitted outputs using R-squared and PRE (Proportional Reduction in Error, also called partial R-squared or partial Eta-squared). |
Authors: | Qingyao Zhang [aut, cre] |
Maintainer: | Qingyao Zhang <[email protected]> |
License: | CC BY 4.0 |
Version: | 2024.11.17 |
Built: | 2024-11-18 07:12:14 UTC |
Source: | CRAN |
Compare lm()'s fitted outputs using PRE and R-squared.
compare_lm( fitC = NULL, fitA = NULL, n = NULL, PC = NULL, PA = NULL, SSEC = NULL, SSEA = NULL )
compare_lm( fitC = NULL, fitA = NULL, n = NULL, PC = NULL, PA = NULL, SSEC = NULL, SSEA = NULL )
fitC |
The result of |
fitA |
The result of |
n |
Sample size of the Model C or Model A. Model C and Model A must use the same sample, and hence have the same sample size. |
PC |
The number of parameters in Model C. |
PA |
The number of parameters in Model A. PA must be larger than PC. |
SSEC |
The Sum of Squared Errors (SSE) of Model C. |
SSEA |
The Sum of Squared Errors of Model A. |
compare_lm()
compare Model A with Model C using PRE (Proportional Reduction in Error) and R-squared. PRE is partial R-squared (called partial Eta-squared in Anova).There are two ways of using compare_lm()
. The first is giving compare_lm()
fitC and fitA. The second is giving n, PC, PA, SSEC, and SSEA. The first way is more convenient, and it minimizes precision loss by omitting copying-and-pasting. If fitC and fitA are not inferior to the intercept-only model, R-squared and Adjusted R-squared are also computed. Note that the F-tests for PRE and R-squared change are equivalent. Please refer to Judd et al. (2017) for more details about PRE.
A data.frame with 3 rows and 8 columns. The first row reports information for Model C, the second for Model A, and the third for the change. The data.frame presents SSE, df of SSE, PRE, the F-test of PRE (F, p), and PRE_adjusted. If fitC and fitA are not inferior to the intercept-only model, R-squared and Adjusted R-squared will also be computed.
Judd, C. M., McClelland, G. H., & Ryan, C. S. (2017). Data analysis: A model comparison approach to regression, ANOVA, and beyond. Routledge.
x1 <- rnorm(193) x2 <- rnorm(193) y <- 0.3 + 0.2*x1 + 0.1*x2 + rnorm(193) dat <- data.frame(y, x1, x2) # Fix intercept to constant 1 using I(). fit1 <- lm(I(y - 1) ~ 0, dat) # Free intercept. fit2 <- lm(y ~ 1, dat) compare_lm(fit1, fit2) # One predictor. fit3 <- lm(y ~ x1, dat) compare_lm(fit2, fit3) # Fix intercept to 0.3 using offset(). intercept <- rep(0.3, 193) fit4 <- lm(y ~ 0 + x1 + offset(intercept), dat) compare_lm(fit4, fit3) # Two predictors. fit5 <- lm(y ~ x1 + x2, dat) compare_lm(fit2, fit5) compare_lm(fit3, fit5) # Fix slope of x2 to 0.05 using offset(). fit6 <- lm(y ~ x1 + offset(0.05*x2), dat) compare_lm(fit6, fit5)
x1 <- rnorm(193) x2 <- rnorm(193) y <- 0.3 + 0.2*x1 + 0.1*x2 + rnorm(193) dat <- data.frame(y, x1, x2) # Fix intercept to constant 1 using I(). fit1 <- lm(I(y - 1) ~ 0, dat) # Free intercept. fit2 <- lm(y ~ 1, dat) compare_lm(fit1, fit2) # One predictor. fit3 <- lm(y ~ x1, dat) compare_lm(fit2, fit3) # Fix intercept to 0.3 using offset(). intercept <- rep(0.3, 193) fit4 <- lm(y ~ 0 + x1 + offset(intercept), dat) compare_lm(fit4, fit3) # Two predictors. fit5 <- lm(y ~ x1 + x2, dat) compare_lm(fit2, fit5) compare_lm(fit3, fit5) # Fix slope of x2 to 0.05 using offset(). fit6 <- lm(y ~ x1 + offset(0.05*x2), dat) compare_lm(fit6, fit5)
Cut-off values of r given the sample size n.
cut_r(n)
cut_r(n)
n |
Sample size of the r. |
Given n and p, t and then r could be determined. The formula used could be found in test_r()
's documentation.
A data.frame including the cut-off values of r at the significance levels of p = 0.1, 0.05, 0.01, 0.001. r with the absolute value larger than the cut-off value is significant at the corresponding significance level.
cut_r(193)
cut_r(193)
A subset of data from a research about depression and coping.
depress
depress
depress
A data frame with 94 rows and 237 columns:
Participant id
Class
Grade
Elite classes
0 = Control group, 1 = Intervention group
0 = girl, 1 = boy
Age in year
Cope scale, Time1, Item1, Problem-focused coping, 1 = very seldom, 5 = very often
Cope scale, Time1, Item3, Avoidance coping
cope scale, Time1, Item5, Emotion-focused coping
Cope scale, Time2, Item1, Problem-focused coping
Depression scale, Time1, Item1, 1 = very seldom, 5 = always
ECR-RS scale, Item1, attachment avoidance, 1 = very disagree, 7 = very agree
ECR-RS scale, Item2, attachment anxiety
Depression, Mean, Time1
Problem-focused coping, Mean, Time1
Emotion-focused coping, Mean, Time1
Avoidance coping, Mean, Time1
Attachment avoidance, Mean
Attachment anxiety, Mean
Keng package.
Scale a vector
Scale(x, expected_M = NULL, expected_SD = NULL, oadvances = NULL)
Scale(x, expected_M = NULL, expected_SD = NULL, oadvances = NULL)
x |
The original vector. |
expected_M |
The expected Mean of the scaled vector. |
expected_SD |
The expected Standard Deviation (unit) of the scaled vector. |
oadvances |
The distance the Origin of x advances by. |
To scale x
, its origin, or unit (expected_SD), or both, could be changed.
If expected_M
= 0 or NULL
, and expected_SD
= NULL
, x
would be mean-centered.
If expected_M
is a non-zero number, and expected_SD
= NULL
, the mean of x
would be transformed to expected_M.
If expected_M
= 0 or NULL
, and expected_SD
= 1, x
would be standardized to be its z-score with M = 0 and SD = 1.
The standardized score is not necessarily the z-score. If neither expected_M
nor expected_SD
is NULL
,
x
would be standardized to be a vector whose mean and standard deviation would be expected_M
and expected_SD
, respectively.
To standardize x
, the mean and standard deviation of x
are needed and computed,
for which the missing values of x
are removed if any.
If oadvances
is not NULL
, the origin of x
will advance with the standard deviation being unchanged.
In this case, Scale()
could be used to pick points in simple slope analysis for moderation models.
Note that when oadvances
is not NULL
, expected_M
and expected_SD
must be NULL.
The scaled vector.
(x <- rnorm(10, 5, 2)) # Mean-center x. Scale(x) # Transform the mean of x to 3. Scale(x, expected_M = 3) # Transform x to its z-score. Scale(x, expected_SD = 1) # Standardize x with *M* = 100 and *SD* = 15. Scale(x, expected_M = 100, expected_SD = 15) # The origin of x advances by 3. Scale(x, oadvances = 3)
(x <- rnorm(10, 5, 2)) # Mean-center x. Scale(x) # Transform the mean of x to 3. Scale(x, expected_M = 3) # Transform x to its z-score. Scale(x, expected_SD = 1) # Standardize x with *M* = 100 and *SD* = 15. Scale(x, expected_M = 100, expected_SD = 15) # The origin of x advances by 3. Scale(x, oadvances = 3)
Test r using the t-test given r and n.
test_r(r, n)
test_r(r, n)
r |
Pearson correlation. |
n |
Sample size of r. |
To test the significance of the r using one-sample t-test, the SE of the r is determined by the following formula: SE = sqrt((1 - r^2)/(n - 2))
.
A data.frame including r, se of r, t, and p.
test_r(0.2, 193)
test_r(0.2, 193)