This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
dat<-read.csv("/Users/jacquelinehogue/Desktop/6132_ass1_2016.csv")
names(dat)
## [1] "sex" "rel_sat" "jealousy" "need_cog"
## [5] "creativity" "open_exp" "extraversion"
head(dat)
## sex rel_sat jealousy need_cog creativity open_exp extraversion
## 1 male 65.38251 67.177493 40.37510 36.01102 27.69277 41.19717
## 2 male 74.39242 4.200623 34.54039 47.53595 26.04504 43.16195
## 3 male 80.12748 25.781636 37.66163 45.79145 49.00146 44.67257
## 4 female 61.92653 30.221668 44.16863 42.70642 51.68647 37.00232
## 5 male 60.05716 40.416728 48.37727 53.08762 39.29398 39.71103
## 6 male 48.78378 29.000114 30.55541 44.52134 30.52985 41.87963
#a) Create a scatterplot (w LOESS line),summarize the relationship bw‘Relationship Satisfaction’ and ‘Jealousy’. No statistics required.
library(car)
scatterplot(dat$rel_sat~dat$jealousy)
#b) Summarize & interpret corr bw:
#i) ‘Creativity and ‘Need for Cognition’; and
#ii) ‘Extraversion’ and ‘Creativity’. Include the confidence intervals.
cor.test(dat$creativity,dat$need_cog)
##
## Pearson's product-moment correlation
##
## data: dat$creativity and dat$need_cog
## t = 1.2184, df = 98, p-value = 0.226
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.07609222 0.31110539
## sample estimates:
## cor
## 0.1221516
cor.test (dat$creativity,dat$extraversion)
##
## Pearson's product-moment correlation
##
## data: dat$creativity and dat$extraversion
## t = 2.4351, df = 98, p-value = 0.0167
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.04453345 0.41577353
## sample estimates:
## cor
## 0.2388622
#c) Is corr bw 'Openness to Experience' and 'Relationship Satisfaction' stronger
#than corr bw 'Openness to Experience' and 'Jealousy'?
library(psych)
##
## Attaching package: 'psych'
## The following object is masked from 'package:car':
##
## logit
rO_Rsat<-cor(dat$open_exp,dat$rel_sat)
rO_Jeal<-cor(dat$open_exp,dat$jealousy)
rRsat_Jeal<-cor(dat$rel_sat,dat$jealousy)
r.test(length(dat$rel_sat),r12=rO_Rsat,r13=rO_Jeal,r23=rRsat_Jeal,twotailed=FALSE)
## Correlation tests
## Call:[1] "r.test(n = 100 , r12 = -0.371023062216691 , r23 = -0.252978162284163 , r13 = -0.277245183516023 )"
## Test of difference between two correlated correlations
## t value -0.64 with probability < 0.26
#d) Is corr bw 'Openness to Experience' and 'Relationship Satisfaction' different in
#males and females? This question should be done “by hand” and in R.
datM<-subset(dat,sex=="male")
datF<-subset(dat,sex=="female")
library(psych)
FO_rel_sat<-cor(datF$open_exp,datF$rel_sat)
FO_rel_sat
## [1] -0.3085393
MO_rel_sat<-cor(datM$open_exp,datM$rel_sat)
MO_rel_sat
## [1] -0.4149565
r.test(n=length(datF$open_exp),n2=length(datM$open_exp),FO_rel_sat,MO_rel_sat)
## Correlation tests
## Call:r.test(n = length(datF$open_exp), r12 = FO_rel_sat, r34 = MO_rel_sat,
## n2 = length(datM$open_exp))
## Test of difference between two independent correlations
## z value 0.59 with probability 0.55
pastecs::stat.desc(datF$open_exp, basic=TRUE, desc=TRUE, norm=FALSE, p=0.95)
## nbr.val nbr.null nbr.na min max
## 50.0000000 0.0000000 0.0000000 10.2587229 55.5193579
## range sum median mean SE.mean
## 45.2606350 1678.1250961 34.5322812 33.5625019 1.5217593
## CI.mean.0.95 var std.dev coef.var
## 3.0580898 115.7875665 10.7604631 0.3206097
pastecs::stat.desc(datM$open_exp, basic=TRUE, desc=TRUE, norm=FALSE, p=0.95)
## nbr.val nbr.null nbr.na min max
## 50.0000000 0.0000000 0.0000000 17.7786766 57.6984021
## range sum median mean SE.mean
## 39.9197254 1762.2566872 34.7930426 35.2451337 1.5273109
## CI.mean.0.95 var std.dev coef.var
## 3.0692461 116.6339221 10.7997186 0.3064173
pastecs::stat.desc(datF$rel_sat, basic=TRUE, desc=TRUE, norm=FALSE, p=0.95)
## nbr.val nbr.null nbr.na min max
## 50.0000000 0.0000000 0.0000000 46.2784780 81.9223899
## range sum median mean SE.mean
## 35.6439118 3262.5435736 66.1167331 65.2508715 1.3443390
## CI.mean.0.95 var std.dev coef.var
## 2.7015504 90.3623710 9.5059124 0.1456825
pastecs::stat.desc(datM$rel_sat, basic=TRUE, desc=TRUE, norm=FALSE, p=0.95)
## nbr.val nbr.null nbr.na min max
## 50.000000 0.000000 0.000000 33.705620 86.937474
## range sum median mean SE.mean
## 53.231854 3140.417845 61.208578 62.808357 1.844670
## CI.mean.0.95 var std.dev coef.var
## 3.707004 170.140424 13.043789 0.207676
describeBy(datF$open_exp)
## Warning in describeBy(datF$open_exp): no grouping variable requested
## vars n mean sd median trimmed mad min max range skew
## 1 1 50 33.56 10.76 34.53 33.85 12.02 10.26 55.52 45.26 -0.24
## kurtosis se
## 1 -0.52 1.52
describeBy(datM$open_exp)
## Warning in describeBy(datM$open_exp): no grouping variable requested
## vars n mean sd median trimmed mad min max range skew kurtosis
## 1 1 50 35.25 10.8 34.79 35.08 12.94 17.78 57.7 39.92 0.14 -1.09
## se
## 1 1.53
describeBy(datF$rel_sat)
## Warning in describeBy(datF$rel_sat): no grouping variable requested
## vars n mean sd median trimmed mad min max range skew kurtosis
## 1 1 50 65.25 9.51 66.12 65.3 9.7 46.28 81.92 35.64 0.02 -0.83
## se
## 1 1.34
describeBy(datM$rel_sat)
## Warning in describeBy(datM$rel_sat): no grouping variable requested
## vars n mean sd median trimmed mad min max range skew
## 1 1 50 62.81 13.04 61.21 63.02 15.79 33.71 86.94 53.23 -0.15
## kurtosis se
## 1 -0.79 1.84
#from Jenkin/Erica code:
#Adjusted code for d) from jenkin's code
file1m_open<- subset(dat$open_exp, dat$sex=="male")
file1f_open<-subset(dat$open_exp, dat$sex=="female")
file1m_sat<- subset(dat$rel_sat, dat$sex=="male")
file1f_sat<- subset(dat$rel_sat, dat$sex=="female")
library(psych)
rm<-cor(file1m_open,file1m_sat)
rf<-cor(file1f_open,file1f_sat)
r.test(n=length(file1m_open),n2=length(file1f_open),rm,rf)
## Correlation tests
## Call:r.test(n = length(file1m_open), r12 = rm, r34 = rf, n2 = length(file1f_open))
## Test of difference between two independent correlations
## z value 0.59 with probability 0.55
#Qe.e) Can relationship satisfaction be predicted from jealousy?
mod1<-lm(dat$rel_sat ~ dat$jealousy)
summary(mod1)
##
## Call:
## lm(formula = dat$rel_sat ~ dat$jealousy)
##
## Residuals:
## Min 1Q Median 3Q Max
## -32.628 -7.986 0.306 7.801 23.147
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 73.00914 3.64237 20.044 <2e-16 ***
## dat$jealousy -0.23934 0.09246 -2.589 0.0111 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 11.11 on 98 degrees of freedom
## Multiple R-squared: 0.064, Adjusted R-squared: 0.05445
## F-statistic: 6.701 on 1 and 98 DF, p-value: 0.0111
mod1
##
## Call:
## lm(formula = dat$rel_sat ~ dat$jealousy)
##
## Coefficients:
## (Intercept) dat$jealousy
## 73.0091 -0.2393
#i) Write out the prediction equation and determine what relationship satisfaction score
#would be predicted for someone with a jealousy score of 50.
#regression equation: y= -0.24x + 73. Solve for x = 50.
Ei<- -.24*50 + 73
Ei
## [1] 61
#ii) By hand, plot the regression line for predicting relationship satisfaction from jealousy.
plot(dat$jealousy,dat$rel_sat, ylab = "rel_sat",xlab = "jealousy")
plot(dat$jealousy,dat$rel_sat)
abline(lm(dat$rel_sat~dat$jealousy),col="blue")
lines(lowess(dat$jealousy,dat$rel_sat),col="red")
legend(1,9,c("ls","lowess"),text.col=c("blue","red"))
#Byx=b. Bo=a.
abline(a=73.009,b=-0.2393)
#iii) Summarize the test of H0: B*YX = 0.
#e) iii. Byx= -0.2393. r^2 = 0.064. sdyrel_sat: 11.42. sdxjeal: 12.07
# Plug in these variables to formula in slide 14 of https://moodle.yorku.ca/moodle/pluginfile.php/1685106/mod_resource/content/2/simple_regression.pdf
describeBy(dat)
## Warning in describeBy(dat): no grouping variable requested
## vars n mean sd median trimmed mad min max range
## sex* 1 100 1.50 0.50 1.50 1.50 0.74 1.00 2.00 1.00
## rel_sat 2 100 64.03 11.42 65.17 64.21 13.07 33.71 86.94 53.23
## jealousy 3 100 37.52 12.07 36.48 37.60 12.34 4.20 67.18 62.98
## need_cog 4 100 40.15 5.87 40.32 40.06 5.25 27.94 57.50 29.56
## creativity 5 100 44.10 5.50 43.59 43.76 4.32 29.32 62.66 33.33
## open_exp 6 100 34.40 10.76 34.79 34.45 12.35 10.26 57.70 47.44
## extraversion 7 100 38.73 5.04 38.72 38.62 6.08 24.33 53.08 28.75
## skew kurtosis se
## sex* 0.00 -2.02 0.05
## rel_sat -0.20 -0.49 1.14
## jealousy -0.07 -0.15 1.21
## need_cog 0.18 0.01 0.59
## creativity 0.66 1.19 0.55
## open_exp -0.05 -0.69 1.08
## extraversion 0.10 -0.16 0.50
cor.test(dat$jealousy, dat$rel_sat)
##
## Pearson's product-moment correlation
##
## data: dat$jealousy and dat$rel_sat
## t = -2.5886, df = 98, p-value = 0.0111
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.42812306 -0.05951744
## sample estimates:
## cor
## -0.2529782
y<- -0.2393/(sqrt((1-0.064)/(100-2))*(11.42/12.07))
y
## [1] -2.587968
# iv) Compute and interpret the confidence interval for BYX
# is the below right? Does he want us to do more than the cor.test?? Compute
#by hand? i still need to interpret this.
library(psychometric)
## Loading required package: multilevel
## Loading required package: nlme
## Loading required package: MASS
##
## Attaching package: 'psychometric'
## The following object is masked from 'package:psych':
##
## alpha
cor.test(dat$jealousy, dat$rel_sat)
##
## Pearson's product-moment correlation
##
## data: dat$jealousy and dat$rel_sat
## t = -2.5886, df = 98, p-value = 0.0111
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.42812306 -0.05951744
## sample estimates:
## cor
## -0.2529782
zprime<-r2z(cor(dat$jealous,dat$rel_sat))
zcan<-zprime/(1/sqrt(length(dat$jealous)-3))
zcan
## [1] -2.546836
cizc<-CIz(zprime,n=length(dat$jealous),level=.95)
z2r(cizc)
## [1] -0.42812306 -0.05951744
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.