library(tidyverse)
Registered S3 methods overwritten by 'dbplyr':
method from
print.tbl_lazy
print.tbl_sql
── Attaching packages ────────────────────────────────── tidyverse 1.3.0 ──
✓ tibble 3.0.3 ✓ dplyr 1.0.2
✓ tidyr 1.1.2 ✓ stringr 1.4.0
✓ readr 1.4.0 ✓ forcats 0.5.0
✓ purrr 0.3.4
── Conflicts ───────────────────────────────────── tidyverse_conflicts() ──
x dplyr::filter() masks stats::filter()
x dplyr::lag() masks stats::lag()
library(psych)
Attaching package: ‘psych’
The following objects are masked from ‘package:ggplot2’:
%+%, alpha
exam <- read_csv("Examdataset .csv")
── Column specification ───────────────────────────────────────────────────
cols(
ability = col_double(),
forceful = col_double(),
analytic = col_double(),
risky = col_double(),
ambitious = col_double(),
hilo = col_double(),
group = col_double()
)
describe(exam)
summary(exam)
ability forceful analytic risky
Min. :1.000 Min. :1.000 Min. :1.000 Min. :1.000
1st Qu.:3.000 1st Qu.:3.000 1st Qu.:4.000 1st Qu.:3.000
Median :5.000 Median :4.000 Median :6.000 Median :4.000
Mean :4.599 Mean :3.951 Mean :5.374 Mean :4.171
3rd Qu.:6.000 3rd Qu.:5.000 3rd Qu.:7.000 3rd Qu.:5.000
Max. :7.000 Max. :7.000 Max. :9.000 Max. :7.000
ambitious hilo group
Min. :1.000 Min. :0.0000 Min. :1.000
1st Qu.:4.000 1st Qu.:0.0000 1st Qu.:2.000
Median :5.000 Median :0.0000 Median :3.000
Mean :5.054 Mean :0.4661 Mean :2.545
3rd Qu.:6.000 3rd Qu.:1.0000 3rd Qu.:4.000
Max. :7.000 Max. :1.0000 Max. :4.000
ggplot(exam, aes(risky)) + geom_boxplot() + facet_wrap(~group)
ggplot(exam, aes(forceful, risky, group = group)) + geom_boxplot() + facet_wrap(~group)
ggplot(exam,aes(x=forceful,y=risky)) + geom_jitter() + facet_wrap(~group)
ggplot(exam,aes(x=forceful,y=risky)) + geom_point() + facet_wrap(~group)
scale_x_log10()+
Error: Incomplete expression: scale_x_log10()+
exam2 <- read.csv("Examdataset2.csv")
boxplot(exam2$rash)
ggplot(exam2, aes(rash)) + geom_boxplot()
cor(exam2)
ability forceful analytic risky ambitious
ability 1.000000000 0.42168091 0.204126413 0.31743910 0.34628805
forceful 0.421680908 1.00000000 0.198085624 0.24070903 0.15246447
analytic 0.204126413 0.19808562 1.000000000 0.14481002 0.17793936
risky 0.317439098 0.24070903 0.144810017 1.00000000 0.33825219
ambitious 0.346288045 0.15246447 0.177939364 0.33825219 1.00000000
hilo 0.003061461 -0.01782069 0.009375494 0.01189650 -0.03999187
group -0.034837121 -0.05927937 -0.035917312 -0.03375605 -0.02795151
rash 0.470168258 0.79480488 0.218169248 0.78033832 0.30972500
hilo group rash
ability 0.003061461 -0.03483712 0.470168258
forceful -0.017820692 -0.05927937 0.794804879
analytic 0.009375494 -0.03591731 0.218169248
risky 0.011896502 -0.03375605 0.780338317
ambitious -0.039991868 -0.02795151 0.309724996
hilo 1.000000000 0.15560199 -0.004043627
group 0.155601994 1.00000000 -0.059299739
rash -0.004043627 -0.05929974 1.000000000
describe(exam2)
cor(exam2)
ability forceful analytic risky ambitious
ability 1.000000000 0.42168091 0.204126413 0.31743910 0.34628805
forceful 0.421680908 1.00000000 0.198085624 0.24070903 0.15246447
analytic 0.204126413 0.19808562 1.000000000 0.14481002 0.17793936
risky 0.317439098 0.24070903 0.144810017 1.00000000 0.33825219
ambitious 0.346288045 0.15246447 0.177939364 0.33825219 1.00000000
hilo 0.003061461 -0.01782069 0.009375494 0.01189650 -0.03999187
group -0.034837121 -0.05927937 -0.035917312 -0.03375605 -0.02795151
rash 0.470168258 0.79480488 0.218169248 0.78033832 0.30972500
hilo group rash
ability 0.003061461 -0.03483712 0.470168258
forceful -0.017820692 -0.05927937 0.794804879
analytic 0.009375494 -0.03591731 0.218169248
risky 0.011896502 -0.03375605 0.780338317
ambitious -0.039991868 -0.02795151 0.309724996
hilo 1.000000000 0.15560199 -0.004043627
group 0.155601994 1.00000000 -0.059299739
rash -0.004043627 -0.05929974 1.000000000
attach(exam2)
The following objects are masked from exam2 (pos = 3):
ability, ambitious, analytic, forceful, group, hilo, rash, risky
The following objects are masked from exam2 (pos = 4):
ability, ambitious, analytic, forceful, group, hilo, rash, risky
The following objects are masked from exam2 (pos = 5):
ability, ambitious, analytic, forceful, group, hilo, rash, risky
The following objects are masked from exam2 (pos = 6):
ability, ambitious, analytic, forceful, group, hilo, rash, risky
The following objects are masked from exam2 (pos = 7):
ability, ambitious, analytic, forceful, group, hilo, rash, risky
The following objects are masked from exam2 (pos = 8):
ability, ambitious, analytic, forceful, group, hilo, rash, risky
The following objects are masked from exam2 (pos = 9):
ability, ambitious, analytic, forceful, group, hilo, rash, risky
The following objects are masked from exam2 (pos = 12):
ability, ambitious, analytic, forceful, group, hilo, rash, risky
The following objects are masked from exam (pos = 13):
ability, ambitious, analytic, forceful, group, hilo, risky
The following objects are masked from exam2 (pos = 19):
ability, ambitious, analytic, forceful, group, hilo, rash, risky
The following objects are masked from exam (pos = 20):
ability, ambitious, analytic, forceful, group, hilo, risky
The following objects are masked from exam (pos = 21):
ability, ambitious, analytic, forceful, group, hilo, risky
The following objects are masked from exam (pos = 22):
ability, ambitious, analytic, forceful, group, hilo, risky
The following objects are masked from exam2 (pos = 23):
ability, ambitious, analytic, forceful, group, hilo, rash, risky
The following objects are masked from exam2 (pos = 33):
ability, ambitious, analytic, forceful, group, hilo, rash, risky
The following objects are masked from exam2 (pos = 34):
ability, ambitious, analytic, forceful, group, hilo, rash, risky
The following objects are masked from exam2 (pos = 35):
ability, ambitious, analytic, forceful, group, hilo, rash, risky
The following objects are masked from exam (pos = 36):
ability, ambitious, analytic, forceful, group, hilo, risky
lr <- lm(forceful~ rash)
summary(lr)
Call:
lm(formula = forceful ~ rash)
Residuals:
Min 1Q Median 3Q Max
-2.88877 -0.42505 0.08704 0.62333 3.11123
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.20798 0.17440 -1.193 0.234
rash 1.02419 0.04082 25.090 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 1.041 on 367 degrees of freedom
Multiple R-squared: 0.6317, Adjusted R-squared: 0.6307
F-statistic: 629.5 on 1 and 367 DF, p-value: < 2.2e-16
ggplot(exam2, aes(x=forceful, y=rash)) + geom_point() + geom_jitter() + geom_smooth(method="lm")
install.packages("epiDisplay")
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.0/epiDisplay_3.5.0.1.tgz'
Content type 'application/x-gzip' length 660576 bytes (645 KB)
==================================================
downloaded 645 KB
The downloaded binary packages are in
/var/folders/0t/jr6fgwg97214m2gdnbw5pbw40000gn/T//RtmpPWkYVu/downloaded_packages
library(epiDisplay)
Loading required package: foreign
Loading required package: survival
Loading required package: MASS
Attaching package: ‘MASS’
The following object is masked from ‘package:dplyr’:
select
Loading required package: nnet
Attaching package: ‘epiDisplay’
The following objects are masked from ‘package:psych’:
alpha, cs, lookup
The following object is masked from ‘package:ggplot2’:
alpha
install.packages("ppcor")
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.0/ppcor_1.1.tgz'
Content type 'application/x-gzip' length 27757 bytes (27 KB)
==================================================
downloaded 27 KB
The downloaded binary packages are in
/var/folders/0t/jr6fgwg97214m2gdnbw5pbw40000gn/T//RtmpPWkYVu/downloaded_packages
install.packages("apaTables")
also installing the dependencies ‘matrixStats’, ‘RcppArmadillo’, ‘zip’, ‘SparseM’, ‘MatrixModels’, ‘conquer’, ‘sp’, ‘data.table’, ‘openxlsx’, ‘minqa’, ‘nloptr’, ‘statmod’, ‘RcppEigen’, ‘carData’, ‘abind’, ‘pbkrtest’, ‘quantreg’, ‘maptools’, ‘rio’, ‘lme4’, ‘car’
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.0/matrixStats_0.57.0.tgz'
Content type 'application/x-gzip' length 2005921 bytes (1.9 MB)
==================================================
downloaded 1.9 MB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.0/RcppArmadillo_0.9.900.3.0.tgz'
Content type 'application/x-gzip' length 1871293 bytes (1.8 MB)
==================================================
downloaded 1.8 MB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.0/zip_2.1.1.tgz'
Content type 'application/x-gzip' length 211635 bytes (206 KB)
==================================================
downloaded 206 KB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.0/SparseM_1.78.tgz'
Content type 'application/x-gzip' length 1096472 bytes (1.0 MB)
==================================================
downloaded 1.0 MB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.0/MatrixModels_0.4-1.tgz'
Content type 'application/x-gzip' length 449286 bytes (438 KB)
==================================================
downloaded 438 KB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.0/conquer_1.0.2.tgz'
Content type 'application/x-gzip' length 872031 bytes (851 KB)
==================================================
downloaded 851 KB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.0/sp_1.4-4.tgz'
Content type 'application/x-gzip' length 1932488 bytes (1.8 MB)
==================================================
downloaded 1.8 MB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.0/data.table_1.13.0.tgz'
Content type 'application/x-gzip' length 2306488 bytes (2.2 MB)
==================================================
downloaded 2.2 MB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.0/openxlsx_4.2.2.tgz'
Content type 'application/x-gzip' length 3028905 bytes (2.9 MB)
==================================================
downloaded 2.9 MB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.0/minqa_1.2.4.tgz'
Content type 'application/x-gzip' length 341728 bytes (333 KB)
==================================================
downloaded 333 KB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.0/nloptr_1.2.2.2.tgz'
Content type 'application/x-gzip' length 945144 bytes (922 KB)
==================================================
downloaded 922 KB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.0/statmod_1.4.34.tgz'
Content type 'application/x-gzip' length 270712 bytes (264 KB)
==================================================
downloaded 264 KB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.0/RcppEigen_0.3.3.7.0.tgz'
Content type 'application/x-gzip' length 4852649 bytes (4.6 MB)
==================================================
downloaded 4.6 MB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.0/carData_3.0-4.tgz'
Content type 'application/x-gzip' length 1819549 bytes (1.7 MB)
==================================================
downloaded 1.7 MB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.0/abind_1.4-5.tgz'
Content type 'application/x-gzip' length 61580 bytes (60 KB)
==================================================
downloaded 60 KB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.0/pbkrtest_0.4-8.6.tgz'
Content type 'application/x-gzip' length 272056 bytes (265 KB)
==================================================
downloaded 265 KB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.0/quantreg_5.73.tgz'
Content type 'application/x-gzip' length 1585756 bytes (1.5 MB)
==================================================
downloaded 1.5 MB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.0/maptools_1.0-2.tgz'
Content type 'application/x-gzip' length 2173904 bytes (2.1 MB)
==================================================
downloaded 2.1 MB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.0/rio_0.5.16.tgz'
Content type 'application/x-gzip' length 503445 bytes (491 KB)
==================================================
downloaded 491 KB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.0/lme4_1.1-23.tgz'
Content type 'application/x-gzip' length 7928269 bytes (7.6 MB)
==================================================
downloaded 7.6 MB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.0/car_3.0-10.tgz'
Content type 'application/x-gzip' length 1561683 bytes (1.5 MB)
==================================================
downloaded 1.5 MB
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.0/apaTables_2.0.5.tgz'
Content type 'application/x-gzip' length 998481 bytes (975 KB)
==================================================
downloaded 975 KB
The downloaded binary packages are in
/var/folders/0t/jr6fgwg97214m2gdnbw5pbw40000gn/T//RtmpPWkYVu/downloaded_packages
library(ppcor)
library(apaTables)
apa.reg.table(lr,filename = "Table1.doc",table.number = 1)
MBESS package needs to be installed to calculate R2 confidence intervals.
Table 1
Regression results using forceful as the criterion
Note. A significant b-weight indicates the beta-weight and semi-partial correlation are also significant.
b represents unstandardized regression weights. beta indicates the standardized regression weights.
sr2 represents the semi-partial correlation squared. r represents the zero-order correlation.
Square brackets are used to enclose the lower and upper limits of a confidence interval.
* indicates p < .05. ** indicates p < .01.
Y = a + bX predicted value of rash = predicted value of rash if forceful is 0 + slope(forceful)
intercept = -0.21, when forceful is 0 rash is -0.21 slope = for every 1 unit increase in forceful, we expect to see a 1.02 increase in rash
library(epiDisplay)
tab1(exam2$group,hilo = TRUE)
exam2$group :
Frequency Percent Cum. percent
1 89 24.1 24.1
2 90 24.4 48.5
3 90 24.4 72.9
4 100 27.1 100.0
Total 369 100.0 100.0
obsf <- matrix(c(89,90,90,100), nrow = 4, ncol = 1, byrow = TRUE)
obsf
[,1]
[1,] 89
[2,] 90
[3,] 90
[4,] 100
exf <- matrix(c(92.25,92.25,92.25,92.25), nrow = 4, ncol = 1, byrow = TRUE)
exf
[,1]
[1,] 92.25
[2,] 92.25
[3,] 92.25
[4,] 92.25
chisq.test(obsf)
Chi-squared test for given probabilities
data: obsf
X-squared = 0.87534, df = 3, p-value = 0.8314
install.packages("DescTools")
library(DescTools)
ContCoef(obsf)
[1] 0.04864748
obsf10 <- matrix(c(89,90), nrow = 2, ncol = 1, byrow = TRUE)
obsf10
[,1]
[1,] 89
[2,] 90
chisq.test(obsf10)
Chi-squared test for given probabilities
data: obsf10
X-squared = 0.0055866, df = 1, p-value = 0.9404
ContCoef(obsf10)
[1] 0.005586505
#install.packages("epitools")
library(epitools)
library(DescTools)
obsf11 <- matrix(c(89,92.25,92.25,90), nrow = 2, ncol = 2)
obsf11
[,1] [,2]
[1,] 89.00 92.25
[2,] 92.25 90.00
oddsratio.wald(obsf11)
'x' has been rounded to integer: Mean relative difference: 0.002710027'x' has been rounded to integer: Mean relative difference: 0.002710027'x' has been rounded to integer: Mean relative difference: 0.002710027
$data
Outcome
Predictor Disease1 Disease2 Total
Exposed1 89.00 92.25 181.25
Exposed2 92.25 90.00 182.25
Total 181.25 182.25 363.50
$measure
odds ratio with 95% C.I.
Predictor estimate lower upper
Exposed1 1.0000000 NA NA
Exposed2 0.9412387 0.6238711 1.420053
$p.value
two-sided
Predictor midp.exact fisher.exact chi.square
Exposed1 NA NA NA
Exposed2 0.7940299 0.8339695 0.7728699
$correction
[1] FALSE
attr(,"method")
[1] "Unconditional MLE & normal approximation (Wald) CI"
We can be 95% confident that group, predicts hilo.