# Demo entry 6660292

m

Submitted by anonymous on Nov 14, 2017 at 22:29
Language: S. Code size: 1.9 kB.

```#Q1(a)
#calculate the lower quartile, median and upper quartile for time,age,meal.cal,wt.loss
summary(lung\$time)
summary(lung\$age)
summary(lung\$meal.cal)
summary(lung\$wt.loss)
#summarry the distribution of other vairables
table(lung\$inst)
table(lung\$status)
table(lung\$sex)
table(lung\$ph.ecog)
table(lung\$ph.karno)
table(lung\$pat.karno)
#Q1(b)
#calculate the mean and standard deviation of survival time for those patients who died
mean(lung[which(lung\$status==2),]\$time)
sd(lung[which(lung\$status==2),]\$time)
#Q1(c)
#the total includes sample individuals with missing data
library(dplyr)
#greater than
p1<-filter(lung,lung\$ph.karno>lung\$pat.karno)
round(length(p1\$ph.karno)/length(lung\$ph.karno),2)#Keep two decimal places
library(dplyr)
#equal to
p2<-filter(lung,lung\$ph.karno==lung\$pat.karno)
round(length(p2\$ph.karno)/length(lung\$ph.karno),2)#Keep two decimal places
library(dplyr)
#better than
p3<-filter(lung,lung\$ph.karno<lung\$pat.karno)
round(length(p3\$ph.karno)/length(lung\$ph.karno),2)#Keep two decimal places
#Q1(d)
#create newage to devide age to 3 categories
lung\$newage<-cut(lung\$age,3,labels=F)
#remove empty value
p4<-lung[complete.cases(lung[,10]),]
#calculate the mean weight loss for each category
aggregate(p4\$wt.loss,by=list(newage=p4\$newage),FUN=mean)
#Q1(e)
#remove empty value
p5<-lung[complete.cases(lung[,9]),]
#calculate range of mean calories consumed across institutions
p6<-aggregate(p5\$meal.cal,by=list(inst=p5\$inst),FUN=mean)
max(p6\$x)-min(p6\$x)
#Q1(f)
#take two variables as x/y axis to draw the distribution
plot(lung\$meal.cal,lung\$time)
#try a linear fit
install.packages("ggplot2")
library(ggplot2)
ggplot(lung)+
geom_point(aes(x=lung\$meal.cal,y=lung\$time))+
theme_bw()+
geom_smooth(aes(x=lung\$meal.cal,y=lung\$time,method="lm")
```

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.