Attachment 'LSA13-PreLecture2-ProblemSet.R'
Download 1 ## @knitr Options, include=F, cache=F, message=F
2 opts_chunk$set(size='footnotesize', eval=T, cache=T, prompt=F, message=F, tidy=F)
3
4
5 ## @knitr LinearityExampleVisualization, fig.show='hold'
6 library(languageR)
7 data(lexdec)
8
9 g = glm(RT ~ I(Frequency^2) + I(Frequency^3), data=lexdec)
10 summary(g)$coefficients
11
12 library(ggplot2)
13 ggplot(lexdec, aes(x=I(Frequency^2), y=RT)) +
14 geom_point() +
15 geom_abline(
16 intercept = coef(g)['(Intercept)'],
17 slope = coef(g)['I(Frequency^2)'],
18 color = "blue")
19
20 ggplot(lexdec, aes(x=I(Frequency^3), y=RT)) +
21 geom_point() +
22 geom_abline(
23 intercept = coef(g)['(Intercept)'],
24 slope = coef(g)['I(Frequency^3)'],
25 color = "red")
26
27
28 ## @knitr LinearityExampleVisualization3, echo=FALSE, out.width='8cm', out.height='8cm'
29 p = function(x) {
30 y = coef(g)['(Intercept)'] +
31 coef(g)['I(Frequency^2)'] * x^2 +
32 coef(g)['I(Frequency^3)'] * x^3
33 return(y)
34 }
35 ggplot(lexdec, aes(x=Frequency, y=RT)) +
36 geom_point() +
37 geom_smooth(method='lm', fill=NA, color="black", linetype="dashed") +
38 stat_function(fun= p, geom="line", color = "green", size=2) +
39 stat_function(fun= function(x) {coef(g)['(Intercept)'] +
40 coef(g)['I(Frequency^2)'] * x^2}, geom="line", color = "blue") +
41 stat_function(fun= function(x) {coef(g)['(Intercept)'] +
42 coef(g)['I(Frequency^3)'] * x^3}, geom="line", color = "red") +
43 annotate("text", label = "x^2", x=7, y=5.9, color = "blue", size=10) +
44 annotate("text", label = "x^3", x=7, y=7, color = "red", size=10)
45
46
47 ## @knitr FrequencyTrialModel, echo=T, results='hide', size='scriptsize'
48 summary(glm(RT ~ Frequency + Trial, data= lexdec))
49
50
51 ## @knitr Centering, echo=T, size='scriptsize'
52 lexdec$cFrequency = lexdec$Frequency - mean(lexdec$Frequency)
53 summary(lexdec[,c('Frequency','cFrequency')])
54
55
56 ## @knitr FrequencyTrialModel2, echo=T, size='scriptsize'
57 summary(glm(RT ~ Frequency * Trial, data= lexdec))$coefficients
58
59
60 ## @knitr Predict, echo=T, eval=F, results='hide', size='scriptsize'
61 # Beyond the coefficients, another interesting thing to compare is
62 # the models predictions (the predicted RTs)
63 g1 = glm(RT ~ Frequency * Trial, data= lexdec)
64 g2 = glm(RT ~ cFrequency * cTrial, data= lexdec)
65
66 # if the models make the same prediction the difference in their
67 # predictions should be zero
68 predict(g1) - predict(g2)
69
70
71 ## @knitr NativeSexModel, echo=T, results='hide', size='scriptsize'
72 summary(glm(RT ~ NativeLanguage * Sex, data= lexdec))$coefficients
73
74
75 ## @knitr ReorderingFactorLevels, echo=T, size='scriptsize'
76 # by default R assumes alphanumeric ordering of factor levels
77 # but we can always change that by explicitly stating what
78 # order we would like:
79 lexdec$NativeLanguage =
80 factor(lexdec$NativeLanguage, levels=c('Other','English'))
81
82 # Btw, c() is simply referring to a vector in R. They can
83 # contain numbers, strings, or any (combination) of other
84 # objects, e.g. c(4, "seven", "Hans the Wurst", c(log(2), "hai"))
85
86
87 ## @knitr AnovaCoding, echo=T, size='scriptsize'
88 # confirm that the default contrast was treatment-coding, e.g.
89 contrasts(lexdec$Sex)
90
91 # change contrasts
92 contrasts(lexdec$Sex) = contr.sum(2)
93 contrasts(lexdec$Sex)
94 # notice that default sum-coding in R assigns 1 to the
95 # alphanumerically first level and -1 to the second level
96
97 # let's label the contrast so that we don't get confused
98 # (cbind() makes a matrix out of multiple vectors; in this
99 # case 1 vector, but it allows us to label the columns/vectors)
100 contrasts(lexdec$Sex) = cbind("Male" = c(-1, 1))
101
102
103 ## @knitr AnovaCoding2, echo=T, size='scriptsize'
104 contrasts(lexdec$Sex) = contr.sum(2) / 2
105 contrasts(lexdec$Sex)
106
107 contrasts(lexdec$NativeLanguage) = contr.sum(2) / 2
108 contrasts(lexdec$NativeLanguage)
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.