File: embedding.R

package info (click to toggle)
car 3.1-3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,520 kB
  • sloc: makefile: 2
file content (73 lines) | stat: -rw-r--r-- 2,358 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
### R code from vignette source 'embedding.Rnw'

###################################################
### code chunk number 1: embedding.Rnw:11-16
###################################################
library(knitr)
library(effects)
library(car)
render_sweave()
options(width=80, digits=4, useFancyQuotes=FALSE, prompt=" ", continue=" ")


###################################################
### code chunk number 2: embedding.Rnw:38-40
###################################################
m2 <- lm(prestige ~ education, data=carData::Prestige)
car::ncvTest(m2, ~ income)


###################################################
### code chunk number 3: embedding.Rnw:43-48 (eval = FALSE)
###################################################
## f3 <- function(meanmod, dta, varmod) {
##   m3 <- lm(meanmod, dta)
##   car::ncvTest(m3, varmod)
##   }
## f3(meanmod=prestige ~ education, dta=carData::Prestige, varmod ~ income)


###################################################
### code chunk number 4: embedding.Rnw:57-67
###################################################
f4 <- function(meanmod, dta, varmod) {
   assign(".dta", dta, envir=.GlobalEnv)
   assign(".meanmod", meanmod, envir=.GlobalEnv)
   m1 <- lm(.meanmod, .dta)
   ans <- car::ncvTest(m1, varmod)
   remove(".dta", envir=.GlobalEnv)
   remove(".meanmod", envir=.GlobalEnv)
   ans
   }
f4(prestige ~ education, carData::Prestige, ~income)


###################################################
### code chunk number 5: embedding.Rnw:74-82 (eval = FALSE)
###################################################
## fc <- function(dta, formula, terms) {
##  if (!require("effects")) stop("effects package unavailable")
##  print(m1 <- lm(formula, dta))
##  Effect(terms, m1)
##  }
## form <- prestige ~ income*type + education
## terms <- c("income", "type")
## fc(carData::Duncan, form, terms)


###################################################
### code chunk number 6: embedding.Rnw:90-101
###################################################
fc.working <- function(dta, formula, terms) {
 if (!require("effects")) stop("effects package unavailable")
 assign(".dta", dta, env=.GlobalEnv)
 print(m1 <- lm(formula, .dta))
 e1 <- Effect(terms, m1)
 remove(".dta", envir=.GlobalEnv)
 e1
}
form <- prestige ~ income*type + education
terms <- c("income", "type")
fc.working(carData::Duncan, form, terms)