### Examples in an introduction and evaluation of extension Program Evaluation

SAEEPER: Goodness-of-Fit Tests because that Nominal Variables

### Packages used in this chapter

The following commands will install this packages if they are not currently installed:

if(!require(dplyr))install.packages("dplyr")if(!require(ggplot2))install.packages("ggplot2")if(!require(grid))install.packages("grid")if(!require(pwr))install.packages("pwr")

When to usage it

Null hypothesis

See the Handbook for info on this topics.

You are watching: Goodness of fit test in r

### How the check works

Chi-square goodness-of-fit example

###--------------------------------------------------------------### Drosophila example, Chi-square goodness-of-fit, p. 46### --------------------------------------------------------------observed = c(770, 230) # observedfrequenciesexpected = c(0.75, 0.25) # expectedproportionschisq.test(x = observed, p = expected)

X-squared = 2.1333, df = 1, p-value = 0.1441

# # #

Post-hoc test

Assumptions

See the Handbook for info on this topics.

### Examples: extrinsichypothesis

### --------------------------------------------------------------### Crossbill example, Chi-square goodness-of-fit, p. 47### --------------------------------------------------------------observed = c(1752, 1895) # observed frequenciesexpected = c(0.5, 0.5) # supposed proportionschisq.test(x = observed, ns = expected)

X-squared = 5.6071, df = 1, p-value = 0.01789

# # #

###--------------------------------------------------------------### Rice example, Chi-square goodness-of-fit, p. 47### --------------------------------------------------------------observed = c(772, 1611, 737)expected = c(0.25, 0.50, 0.25)chisq.test(x = observed, ns = expected)

X-squared = 4.1199, df = 2, p-value = 0.1275

# # #

###--------------------------------------------------------------### Bird foraging example, Chi-square goodness-of-fit, pp. 47–48### --------------------------------------------------------------observed = c(70, 79, 3, 4)expected = c(0.54, 0.40, 0.05, 0.01)chisq.test(x = observed, p = expected)

X-squared = 13.5934, df = 3, p-value = 0.0035

# # #

### Example: intrinsichypothesis

###--------------------------------------------------------------### Intrinsic example, Chi-square goodness-of-fit, p. 48### --------------------------------------------------------------observed = c(1203, 2919, 1678)expected.prop = c(0.211, 0.497, 0.293)expected.count = sum(observed)*expected.propchi2 = sum((observed- expected.count)^2/ expected.count)chi2

<1> 1.082646

pchisq(chi2, df=1, lower.tail=FALSE)

<1> 0.2981064

# # #

### Graphing the results

The very first example below will usage the barplot functionin the indigenous graphics parcel to produce a basic plot. An initial we willcalculate the it was observed proportions and also then copy those results right into a matrixformat for plotting. We’ll call this matrix Matriz. View the “Chi-squareTest of Independence” section for a couple of notes on developing matrices.

The second example supplies the package ggplot2, and usesa data structure instead that a matrix. The data framework is named Forage. Forthis example, the code calculates confidence intervals and adds them come thedata frame. This code might be skipped if those values were determinedmanually and put into a data framework from i beg your pardon the plot could be generated.

Sometimes components will need to have the stimulate of theirlevels mentioned for ggplot2 to placed them in the exactly order ~ above theplot, together in the 2nd example. Otherwise R will alphabetize levels.

Simple bar plot with barplot

###--------------------------------------------------------------### straightforward bar plot of proportions, p. 49### supplies data in a matrix format### --------------------------------------------------------------

observed = c(70, 79, 3, 4)expected = c(0.54, 0.40, 0.05, 0.01)total = sum(observed)observed.prop = it was observed / totalobserved.prop

Douglas fir Ponderosa pine cool fir western larch

barplot(Matriz, beside=TRUE, legend=TRUE, ylim=c(0, 0.6), xlab="Tree species", ylab="Foraging proportion")

# # #

Bar plot v confidence intervals v ggplot2

The plot below is a bar char through confidence intervals. Thecode calculates trust intervals. This code might be skipped if thosevalues were determined manually and also put in to a data structure from i m sorry the plotcould it is in generated.

Sometimes determinants will require to have actually the stimulate of theirlevels specified for ggplot2 to placed them in the exactly order on theplot. Otherwise R will alphabetize levels.

### --------------------------------------------------------------### Graph example, Chi-square goodness-of-fit, p. 49### utilizing ggplot2### Plot adapted from:### shinyapps.stat.ubc.ca/r-graph-catalog/ ### --------------------------------------------------------------Input =("Tree worth Count full Proportion Expected"Douglas fir" it was observed 70 156 0.4487 0.54"Douglas fir" supposed 54 100 0.54 0.54"Ponderosa pine" observed 79 156 0.5064 0.40"Ponderosa pine" meant 40 100 0.40 0.40"Grand fir" observed 3 156 0.0192 0.05"Grand fir" supposed 5 100 0.05 0.05"Western larch" it was observed 4 156 0.0256 0.01"Western larch" supposed 1 100 0.01 0.01") Forage = read.table(textConnection(Input),header=TRUE)### specify the bespeak of element levels. Otherwise Rwill alphabetize them.library(dplyr)Forage = mutate(Forage, Tree = factor(Tree, levels=unique(Tree)), worth = factor(Value, levels=unique(Value)) ) ### include confidence intervalsForage = mutate(Forage, low.ci = apply(Forage, 1, function(x) binom.test(x<"Count">, x<"Total">,x<"Expected"> )\$ conf.int<1>), upper.ci = apply(Forage, 1, function(x) binom.test(x<"Count">, x<"Total">,x<"Expected"> )\$ conf.int<2>) )Forage\$ low.ci = 0Forage\$ upper.ci = 0Forage

Tree value Count total Proportion expected low.ci upper.ci

1 Douglas firObserved 70 156 0.4487 0.54 0.369115906 0.53030534

2 Douglas firExpected 54 100 0.5400 0.54 0.000000000 0.00000000

3 Ponderosa pineObserved 79 156 0.5064 0.40 0.425290653 0.58728175

4 Ponderosa pineExpected 40 100 0.4000 0.40 0.000000000 0.00000000

8 western larchExpected 1 100 0.0100 0.01 0.000000000 0.00000000

### Plot adapted from:### shinyapps.stat.ubc.ca/r-graph-catalog/ library(ggplot2)library(grid)ggplot(Forage, aes(x = Tree, y = Proportion, fill = Value, ymax=upper.ci, ymin=low.ci)) + geom_bar(stat="identity", place = "dodge", width= 0.7) + geom_bar(stat="identity", position = "dodge", colour = "black", width = 0.7, show_guide = FALSE) + scale_y_continuous(breaks = seq(0, 0.60, 0.1), limits = c(0, 0.60), increase = c(0, 0)) + scale_fill_manual(name = "Count type" , values = c("grey80", "grey30"), labels = c("Observed value", "Expected value")) + geom_errorbar(position=position_dodge(width=0.7), width=0.0, size=0.5, color="black") + labs(x = "Tree species", y = "Foraging proportion") + ## ggtitle("Main title") + theme_bw() + theme(panel.grid.major.x = element_blank(), panel.grid.major.y = element_line(colour = "grey50"), plot.title = element_text(size = rel(1.5), challenge = "bold", vjust = 1.5), axis.title = element_text(face = "bold"), legend.position = "top", legend.title = element_blank(), legend.key.size = unit(0.4, "cm"), legend.key = element_rect(fill = "black"), axis.title.y = element_text(vjust= 1.8), axis.title.x = element_text(vjust= -0.5) )

# # #

Bar plot of proportions vs. Categories. Error bars show 95%confidence intervals because that each it was observed proportion.

See more: Does Phoenix Raceway Have Lights Archives, The History Of Phoenix Raceway

Similar tests

Chi-square vs. G–test

See the Handbook for information on these topics. The specific test of goodness-of-fit, the G-test that goodness-of-fit,and the exact test of goodness-of-fit tests are defined elsewhere inthis book.

### How to perform the test

Chi-square goodness-of-fit example

###--------------------------------------------------------------### Pea shade example, Chi-square goodness-of-fit, pp. 50–51### --------------------------------------------------------------observed = c(423, 133) meant = c(0.75, 0.25) chisq.test(x = observed, ns = expected)

X-squared = 0.3453, df = 1, p-value = 0.5568

# # #

### Power analysis

Power evaluation for chi-square goodness-of-fit

###--------------------------------------------------------------### strength analysis, Chi-square goodness-of-fit, snapdragons, p. 51### --------------------------------------------------------------library(pwr)P0 = c(0.25, 0.50, 0.25)P1 = c(0.225, 0.55, 0.225) effect.size = ES.w1(P0, P1) levels = length(P0) - 1pwr.chisq.test( w=effect.size, N=NULL, # complete number ofobservations df=degrees, power=0.80, # 1 minus kind IIprobability sig.level=0.05) # type Iprobability