#install.packages("psych")
#install.packages("tidyverse")
library(psych)
library(tidyverse)
Registered S3 methods overwritten by 'dbplyr':
method from
print.tbl_lazy
print.tbl_sql
── Attaching packages ─────────────────────────────────────────────── tidyverse 1.3.0 ──
✓ ggplot2 3.3.2 ✓ purrr 0.3.4
✓ tibble 3.0.3 ✓ dplyr 1.0.2
✓ tidyr 1.1.2 ✓ stringr 1.4.0
✓ readr 1.4.0 ✓ forcats 0.5.0
── Conflicts ────────────────────────────────────────────────── tidyverse_conflicts() ──
x ggplot2::%+%() masks psych::%+%()
x ggplot2::alpha() masks psych::alpha()
x dplyr::filter() masks stats::filter()
x dplyr::lag() masks stats::lag()
Healthdataset <- read_csv("healthdata .csv")
── Column specification ────────────────────────────────────────────────────────────────
cols(
age = col_double(),
sex = col_double(),
physhealrth = col_double(),
memory = col_double(),
memory2 = col_double(),
visitGP = col_double(),
visitER = col_double(),
vigorousact = col_double(),
moderateact = col_double(),
walking = col_double(),
sleep = col_double(),
sleeptrouble = col_double()
)
visitGPonPH<-lm(physhealrth ~visitGP, data = Healthdataset)
summary(visitGPonPH)
Call:
lm(formula = physhealrth ~ visitGP, data = Healthdataset)
Residuals:
Min 1Q Median 3Q Max
-3.2825 -2.0778 0.8681 1.0187 4.4822
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 4.28248 0.04811 89.02 <2e-16 ***
visitGP -0.15059 0.01206 -12.49 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 1.729 on 2210 degrees of freedom
(3 observations deleted due to missingness)
Multiple R-squared: 0.06592, Adjusted R-squared: 0.06549
F-statistic: 156 on 1 and 2210 DF, p-value: < 2.2e-16
confint(visitGPonPH)
2.5 % 97.5 %
(Intercept) 4.1881449 4.3768179
visitGP -0.1742356 -0.1269415
allpredictors<-lm(physhealrth ~ memory + visitGP + visitER + vigorousact + moderateact + walking, data = Healthdataset)
summary(allpredictors)
Call:
lm(formula = physhealrth ~ memory + visitGP + visitER + vigorousact +
moderateact + walking, data = Healthdataset)
Residuals:
Min 1Q Median 3Q Max
-3.5097 -1.8711 0.8128 1.0845 4.3657
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 4.675755 0.132445 35.303 < 2e-16 ***
memory -0.158864 0.039952 -3.976 7.22e-05 ***
visitGP -0.138165 0.012405 -11.138 < 2e-16 ***
visitER -0.180477 0.070784 -2.550 0.0108 *
vigorousact 0.007470 0.018991 0.393 0.6941
moderateact -0.000933 0.014150 -0.066 0.9474
walking -0.006694 0.014932 -0.448 0.6540
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 1.723 on 2194 degrees of freedom
(14 observations deleted due to missingness)
Multiple R-squared: 0.07562, Adjusted R-squared: 0.07309
F-statistic: 29.91 on 6 and 2194 DF, p-value: < 2.2e-16
confint(allpredictors)
2.5 % 97.5 %
(Intercept) 4.41602370 4.93548591
memory -0.23721232 -0.08051587
visitGP -0.16249162 -0.11383820
visitER -0.31928748 -0.04166594
vigorousact -0.02977131 0.04471189
moderateact -0.02868226 0.02681628
walking -0.03597684 0.02258907
smallsleep <- read_csv("smallsleep .csv")
── Column specification ────────────────────────────────────────────────────────────────
cols(
sex = col_double(),
sleep = col_double(),
sleeptrouble = col_double()
)
smallsleep <- mutate(smallsleep,
sleep.f = factor(sleep,
levels = c(1,2,3,4),
labels = c("1", "2", "3", "4")))
smallsleep <- mutate(smallsleep,
sex.f = factor(sex,
levels = c(1,2),
labels = c("female", "male")))
SxSA2 <- select(smallsleep, sleep.f, sex.f, sleeptrouble)
SSA <- lm(sleeptrouble ~sleep.f * sex.f, data = SxSA2)
anova(SSA)
Analysis of Variance Table
Response: sleeptrouble
Df Sum Sq Mean Sq F value Pr(>F)
sleep.f 3 0.36 0.12000 0.2903 0.83236
sex.f 1 0.32 0.32000 0.7742 0.38002
sleep.f:sex.f 3 2.68 0.89333 2.1613 0.09397 .
Residuals 192 79.36 0.41333
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
aggregate(x=SxSA2$sleeptrouble, by=list(SxSA2$sleep.f, SxSA2$sex.f), FUN=mean)
NA
##GM = 50 ##TOTss = 25, 000 + 465 = 25, 465 ##SSB = S ni (Mi – GM)2 ### = 50(35-50)2 + 50(45-50)2 + 50(65-50)2 + 50(55-50)2 ###= 11250 + 1250 + 11250 + 1250 = 25,000 ##SSW = S (Y – Mi)2 ### = (50-35)2 + (50-45)2 + (50-65)2 + (50-55)2 ###= 465 ##eta2 = 25,000 / 25, 465 = .98, large effect – 98% of the variability in scores is due to treatment and 2% of the variability is due to other things. ##• eta2 = 25,000 / 25, 465 = .98, large effect – 98% of the variability in scores is due to treatment and 2% of the variability is due to other things. ### T4 (55)1 ### T1 (35)-.5 ### T2 (45)-.5 ### C = (((35+45-110)2)*50)/4 = 11250
health2 <- read_csv("healthdata2 .csv")
── Column specification ────────────────────────────────────────────────────────────────
cols(
control = col_double(),
autonomy = col_double(),
pleasure = col_double(),
worry = col_double(),
lonley = col_double(),
alcoholuse = col_double(),
mentalhealth = col_double()
)
install.packages("olsrr")
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.0/olsrr_0.5.3.tgz'
Content type 'application/x-gzip' length 2205054 bytes (2.1 MB)
==================================================
downloaded 2.1 MB
The downloaded binary packages are in
/var/folders/0t/jr6fgwg97214m2gdnbw5pbw40000gn/T//Rtmp7HAAsQ/downloaded_packages
library(olsrr)
Registered S3 method overwritten by 'data.table':
method from
print.data.table
Attaching package: ‘olsrr’
The following object is masked from ‘package:datasets’:
rivers
CAPW <- lm(lonley ~ control + autonomy + pleasure + worry, data = health2)
ols_regress(CAPW)
Model Summary
--------------------------------------------------------------
R 0.626 RMSE 1.563
R-Squared 0.392 Coef. Var 95.080
Adj. R-Squared 0.391 MSE 2.442
Pred R-Squared 0.387 MAE 1.168
--------------------------------------------------------------
RMSE: Root Mean Square Error
MSE: Mean Square Error
MAE: Mean Absolute Error
ANOVA
-----------------------------------------------------------------------
Sum of
Squares DF Mean Square F Sig.
-----------------------------------------------------------------------
Regression 2213.894 4 553.473 226.602 0.0000
Residual 3429.253 1404 2.442
Total 5643.147 1408
-----------------------------------------------------------------------
Parameter Estimates
-----------------------------------------------------------------------------------------
model Beta Std. Error Std. Beta t Sig lower upper
-----------------------------------------------------------------------------------------
(Intercept) 8.485 0.424 20.003 0.000 7.653 9.317
control -0.302 0.026 -0.320 -11.629 0.000 -0.353 -0.251
autonomy -0.031 0.022 -0.037 -1.422 0.155 -0.073 0.012
pleasure -0.322 0.027 -0.280 -11.793 0.000 -0.376 -0.269
worry 0.048 0.006 0.173 7.393 0.000 0.035 0.061
-----------------------------------------------------------------------------------------
C <- lm(lonley ~ control,data = health2)
ols_regress(C)
Model Summary
---------------------------------------------------------------
R 0.538 RMSE 1.709
R-Squared 0.290 Coef. Var 101.393
Adj. R-Squared 0.289 MSE 2.921
Pred R-Squared 0.287 MAE 1.314
---------------------------------------------------------------
RMSE: Root Mean Square Error
MSE: Mean Square Error
MAE: Mean Absolute Error
ANOVA
-----------------------------------------------------------------------
Sum of
Squares DF Mean Square F Sig.
-----------------------------------------------------------------------
Regression 1791.558 1 1791.558 613.442 0.0000
Residual 4395.354 1505 2.921
Total 6186.912 1506
-----------------------------------------------------------------------
Parameter Estimates
-----------------------------------------------------------------------------------------
model Beta Std. Error Std. Beta t Sig lower upper
-----------------------------------------------------------------------------------------
(Intercept) 6.423 0.196 32.724 0.000 6.038 6.808
control -0.513 0.021 -0.538 -24.768 0.000 -0.554 -0.473
-----------------------------------------------------------------------------------------
APW <- lm(lonley ~ autonomy + pleasure + worry, data = health2)
ols_regress(APW)
Model Summary
--------------------------------------------------------------
R 0.580 RMSE 1.644
R-Squared 0.336 Coef. Var 98.994
Adj. R-Squared 0.335 MSE 2.701
Pred R-Squared 0.331 MAE 1.241
--------------------------------------------------------------
RMSE: Root Mean Square Error
MSE: Mean Square Error
MAE: Mean Absolute Error
ANOVA
-----------------------------------------------------------------------
Sum of
Squares DF Mean Square F Sig.
-----------------------------------------------------------------------
Regression 1971.845 3 657.282 243.337 0.0000
Residual 3892.316 1441 2.701
Total 5864.162 1444
-----------------------------------------------------------------------
Parameter Estimates
-----------------------------------------------------------------------------------------
model Beta Std. Error Std. Beta t Sig lower upper
-----------------------------------------------------------------------------------------
(Intercept) 7.730 0.431 17.921 0.000 6.884 8.576
autonomy -0.156 0.020 -0.188 -7.898 0.000 -0.194 -0.117
pleasure -0.391 0.028 -0.338 -14.038 0.000 -0.445 -0.336
worry 0.064 0.007 0.232 9.889 0.000 0.052 0.077
-----------------------------------------------------------------------------------------
#install.packages("ppcor")
library(ppcor)
Loading required package: MASS
Attaching package: ‘MASS’
The following object is masked from ‘package:olsrr’:
cement
The following object is masked from ‘package:dplyr’:
select
alCpw <- lm(lonley ~ autonomy + pleasure + worry, health2)
summary(alCpw)
Call:
lm(formula = lonley ~ autonomy + pleasure + worry, data = health2)
Residuals:
Min 1Q Median 3Q Max
-4.8425 -1.0486 -0.3612 0.7710 6.8118
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 7.730120 0.431350 17.921 < 2e-16 ***
autonomy -0.155769 0.019722 -7.898 5.56e-15 ***
pleasure -0.390549 0.027821 -14.038 < 2e-16 ***
worry 0.064290 0.006501 9.889 < 2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 1.644 on 1441 degrees of freedom
(263 observations deleted due to missingness)
Multiple R-squared: 0.3363, Adjusted R-squared: 0.3349
F-statistic: 243.3 on 3 and 1441 DF, p-value: < 2.2e-16