Attachment 'APS-hw1.R'

Download

   1 ## Regression Course, Session 1 Homework
   2 ## Anne Pier Salverda, 05/30/2008
   3 
   4 
   5 #Gelman, exercise 3a
   6 
   7 library(arm)
   8 var1<-rnorm(1000,0,1)
   9 var2<-rnorm(1000,0,1)
  10 test.lm=lm(var1~var2)
  11 summary(test.lm)
  12 
  13 # there is no signiciant effect of var2 on var1
  14 
  15 
  16 #Gelman, exercise 3b
  17 
  18 z.scores <- rep (NA,100)
  19 for (k in 1:100) {
  20 	var1<-rnorm(1000,0,1)
  21 	var2<-rnorm(1000,0,1)
  22 	fit<-lm(var2~var1)
  23 	z.scores[k]=coef(fit)[2]/se.coef(fit)[2]
  24 	}
  25 	
  26 length(z.scores[z.scores<(-1.96)|z.scores>1.96])
  27 
  28 # 8 z scores are statistically significant!
  29 
  30 
  31 #Gelman, exercise 5
  32 
  33 beauty<-read.csv("~/Desktop/ProfEvaltnsBeautyPublic.csv")
  34 
  35 # visualization and exploration; most basic model
  36 plot(beauty$btystdave,beauty$courseevaluation)
  37 abline(coef(beauty.lm))
  38 beauty.lm=lm(courseevaluation~btystdave,data=beauty)
  39 summary(beauty.lm)
  40 
  41 Gelman, exercise 5a
  42 
  43 # Model with a bunch of controls
  44 beauty.lm2=lm(courseevaluation~btystdave+tenured+minority+age+nonenglish+tenuretrack+onecredit+female+students,data=beauty)
  45 summary(beauty.lm2)
  46 plot(beauty.lm2)
  47 
  48 # explanation of significant coefficients:
  49 #
  50 # "all else being equal, ..."
  51 #
  52 # btystdave: an increase in 1 unit on the beauty scale is expected to result in an increase in 0.16 on the course evaluation score
  53 # minority: a teacher who is a minority is expected to get a course evaluation score that is 0.16 lower than a teacher who is not a minority
  54 # nonenglish: a teacher who is not a native speaker of English is expected to get a course evaluation score that is 0.22 lower than a teacher who is a native speaker of English
  55 # onecredit: if the course is one credit, the course evaluation score is expected to be 0.61 higher than when the course is >1 credit
  56 # female: if the teacher is female, they are expected to get a course evaluation that is 0.19 lower than if the teacher is male
  57 
  58 #Gelman, exercise 5b
  59 
  60 # Simple model with interaction
  61 beauty.lm4=lm(courseevaluation~btystdave*female,data=beauty)
  62 summary(beauty.lm4)
  63 
  64 # predictors: constant, btystdave, female, btystdave*female
  65 # units: btystdave, female
  66 
  67 # coefficients: as in previous models. the interaction term shows that the gender of the teacher has an influence on how beauty affects courseevaluation
  68 
  69 # More complex model with interaction
  70 beauty.lm3=lm(courseevaluation~btystdave*female+tenured+minority+age+nonenglish+tenuretrack+onecredit+students,data=beauty)
  71 summary(beauty.lm3)
  72 
  73 # predictors: constant; btystdave, female, tenured, minority, age, nonenglish, tenuretrack, onecredit, students; btstdave*female
  74 # units: all of the above except for the constant and interaction
  75 
  76 
  77 # Baayen, exercise 3
  78 
  79 plot(density(durationsGe$DurationOfPrefix))
  80 # skewed
  81 
  82 plot(density(durationsGe$Frequency))
  83 # skewed!
  84 
  85 durationsGe$logduration=log(durationsGe$DurationOfPrefix)
  86 plot(density(durationsGe$logduration))
  87 # looks better
  88 
  89 durationsGe$logfrequency=log(durationsGe$Frequency)
  90 plot(density(durationsGe$logfrequency))
  91 # looks somewhat better
  92 
  93 durationsGe.lm=lm(logduration~logfrequency,data=durationsGe)
  94 summary(durationsGe.lm)
  95 # the frequency of a word with the prefix "ge" affects its duration: the higher the word's frequency, the shorter its duration
  96 
  97 
  98 # Baayen, exercise 7
  99 
 100 durations.subset=subset(durationsOnt,DurationPrefixNasal>0)
 101 durations.subset$logdurationprefixnasal=log(durations.subset$DurationPrefixNasal)
 102 nasalduration.lm=lm(logdurationprefixnasal~Frequency+PlosivePresent,data=durations.subset)
 103 summary(nasalduration.lm)
 104 # the duration of the nasal is affected by the presence of the following plosive, but not significantly by the word's frequency. when the plosive is present, the duration of the word is shorter than when the plosive is absent.  Really?
 105 
 106 mean(durations.subset$DurationPrefixNasal[durations.subset$PlosivePresent=="yes"])
 107 mean(durations.subset$DurationPrefixNasal[durations.subset$PlosivePresent=="no"])

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.
  • [get | view] (2021-04-22 12:55:33, 3.7 KB) [[attachment:APS-hw1.R]]
  • [get | view] (2021-04-22 12:55:33, 7.2 KB) [[attachment:BenVanDurme-hw1.R]]
  • [get | view] (2021-04-22 12:55:33, 8.8 KB) [[attachment:TingQian-hw1.R]]
  • [get | view] (2021-04-22 12:55:33, 3043.5 KB) [[attachment:attention-procedure.ppt]]
  • [get | view] (2021-04-22 12:55:33, 13.5 KB) [[attachment:attention-r-commands.R]]
  • [get | view] (2021-04-22 12:55:33, 86.3 KB) [[attachment:attention-r-data.csv]]
  • [get | view] (2021-04-22 12:55:33, 208.5 KB) [[attachment:case-influence.ppt]]
  • [get | view] (2021-04-22 12:55:33, 11.7 KB) [[attachment:contrast-coding.R]]
  • [get | view] (2021-04-22 12:55:33, 11.7 KB) [[attachment:contrast-coding2.R]]
  • [get | view] (2021-04-22 12:55:33, 12.8 KB) [[attachment:kidiq.dta]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.

MoinMoin Appliance - Powered by TurnKey Linux