attach(mtcars)
opar = par(no.readnoly=TRUE)
d = mtcars[c("mpg","hp","wt")]
head(d)
summary(d)
plot(density(mpg))
library(psych)
describe(d)
aggregate(d,by=list(am=am),mean)
dstats <- function(x){
c(mean=sapply(x,mean),sum=sapply(x,sum))
}
tapply(d$mpg,factor(am),dstats)
by(d,factor(am),dstats)
by(d,factor(am),summary)
library(doBy)
dstats <- function(x){
c(mean=mean(x),sum=sum(x))
}
summaryBy(mpg+hp+wt~am,data=mtcars,FUN=dstats)
library(psych)
describeBy(d,am)
library(reshape)
dstats <- function(x){
c(mean=mean(x),sum=sum(x),length=length(x))
}
dfm = melt(mtcars,measure.vars = c("mpg","hp","wt"),
id.vars=c("am","cyl"))
cast(dfm,am+cyl+variable~.,dstats)
table(var1,var2)
xtabs(formula,data)
prop.table(table,margins)
margin.table(table,margin)
addmargins(table,margins)
ftable(table)
table(cyl)
prop.table(table(cyl))
table(cyl,am)
mytable = xtabs(~am+cyl,data=mtcars)
margin.table(mytable,margin = 1)
prop.table(mytable,margin = 1)
prop.table(mytable,margin = 2)
prop.table(mytable)
addmargins(mytable)
addmargins(prop.table(mytable))
addmargins(prop.table(mytable,1),2)
addmargins(prop.table(mytable,2),1)
install.packages("gmodels")
library(gmodels)
CrossTable(am,cyl)
mytable = xtabs(~am+cyl+gear,data=mtcars)
mytable
ftable(mytable)
margin.table(mytable,1)
margin.table(mytable,3)
margin.table(mytable,c(1,3))
ftable(prop.table(mytable,c(1,2)))
ftable(addmargins(prop.table(mytable,c(1,2)),3) )
table2flat <- function(mytable) {
df = as.data.frame(mytable)
rows = dim(df)[1]
cols = dim(df)[2]
x = NULL
counts=0
for (i in 1:rows) {
for (j in 1:df$freq[i]) {
counts = counts+1
row = df[i,c(1:(cols-1))]
x = rbind(x,row)
}
print(c(i,counts))
}
row.names(x) <- c(1:dim(x)[1])
return (list(x,counts))
}
treatment = rep(c("Placebo","Treated"),times=3)
improved = rep(c("None","Some","Marked"),each=2)
freq = c(29,13,7,17,7,21)
mytable = as.data.frame(cbind(treatment,improved,freq))
mytable$freq = as.numeric(as.character(mytable$freq))
mydata = table2flat(mytable)
library(vcd)
mytable = xtabs(~Treatment+Improved,data=Arthritis)
chisq.test(mytable)
mytable = xtabs(~Sex+Improved,data=Arthritis)
chisq.test(mytable)
mytable = xtabs(~Treatment+Improved,data=Arthritis)
fisher.test(mytable)
mytable = xtabs(~Treatment+Improved+Sex,data=Arthritis)
mantelhaen.test(mytable)
library(vcd)
mytable = xtabs(~Treatment+Improved,data=Arthritis)
chisq.test(mytable)
assocstats(mytable)
states=state.x77[,1:6]
cov(states)
cor(states)
cor(states,method="spearman")
x = states[,c("Population","Income","HS Grad")]
y = states[,c("Life Exp","Murder")]
cor(x,y)
install.packages("ggm")
library(ggm)
pcor(c(1,5,2,3,6),cov(states))
cor.test(states[,3],states[,5])
library(psych)
corr.test(states,use="complete")
library(MASS)
t.test(Prob~So,data=UScrime)
library(MASS)
sapply(UScrime[c("U1","U2")],function(x) (c(mean=mean(x),sd=sd(x))) )
with(UScrime, t.test(U1,U2,paired=TRUE))
ANOVA分析
with(UScrime,by(Prob,So,median))
wilcox.test(Prob~So,data=UScrime)
sapply(UScrime[c("U1","U2")],median)
with(UScrime, wilcox.test(U1,U2,paired = TRUE))
class = state.region
var = state.x77[,c("Illiteracy")]
mydata = as.data.frame(cbind(class,var))
rm(class,var)
install.packages("npmc")
library(npmc)
summary(npmc(mydata),type="BF")