99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀 R語言 基本數(shù)據(jù)分析
R語言 基本數(shù)據(jù)分析
2016-01-13
收藏

本文基于R語言進(jìn)行基本數(shù)據(jù)統(tǒng)計(jì)分析,包括基本作圖,線性擬合,邏輯回歸,bootstrap采樣和Anova方差分析的實(shí)現(xiàn)及應(yīng)用。

不多說,直接上代碼,代碼中有注釋。


1. 基本作圖(盒圖,qq圖)

#basic plot
boxplot(x)
qqplot(x,y)


2.  線性擬合

#linear regression
n = 10
x1 = rnorm(n)#variable 1
x2 = rnorm(n)#variable 2
y = rnorm(n)*3
mod = lm(y~x1+x2)
model.matrix(mod) #erect the matrix of mod
plot(mod) #plot residual and fitted of the solution, Q-Q plot and cook distance
summary(mod) #get the statistic information of the model
hatvalues(mod) #very important, for abnormal sample detection


3. 邏輯回歸

#logistic regression
x <- c(0, 1, 2, 3, 4, 5)
y <- c(0, 9, 21, 47, 60, 63) # the number of successes
n <- 70 #the number of trails
z <- n - y #the number of failures
b <- cbind(y, z) # column bind
fitx <- glm(b~x,family = binomial) # a particular type of generalized linear model
print(fitx)

plot(x,y,xlim=c(0,5),ylim=c(0,65)) #plot the points (x,y)

beta0 <- fitx$coef[1]
beta1 <- fitx$coef[2]
fn <- function(x) n*exp(beta0+beta1*x)/(1+exp(beta0+beta1*x))
par(new=T)
curve(fn,0,5,ylim=c(0,60)) # plot the logistic regression curve



3. Bootstrap采樣
# bootstrap
# Application: 隨機(jī)采樣,獲取最大eigenvalue占所有eigenvalue和之比,并畫圖顯示distribution
dat = matrix(rnorm(100*5),100,5)
 no.samples = 200 #sample 200 times
# theta = matrix(rep(0,no.samples*5),no.samples,5)
 theta =rep(0,no.samples*5);
 for (i in 1:no.samples)
{
    j = sample(1:100,100,replace = TRUE)#get 100 samples each time
   datrnd = dat[j,]; #select one row each time
   lambda = princomp(datrnd)$sdev^2; #get eigenvalues
#   theta[i,] = lambda;
   theta[i] = lambda[1]/sum(lambda); #plot the ratio of the biggest eigenvalue
}

# hist(theta[1,]) #plot the histogram of the first(biggest) eigenvalue
hist(theta); #plot the percentage distribution of the biggest eigenvalue
sd(theta)#standard deviation of theta

#上面注釋掉的語句,可以全部去掉注釋并將其下一條語句注釋掉,完成畫最大eigenvalue分布的功能


4. ANOVA方差分析

#Application:判斷一個(gè)自變量是否有影響 (假設(shè)我們喂3種維他命給3頭豬,想看喂維他命有沒有用)
# 
y = rnorm(9); #weight gain by pig(Yij, i is the treatment, j is the pig_id), 一般由用戶自行輸入
#y = matrix(c(1,10,1,2,10,2,1,9,1),9,1)
Treatment <- factor(c(1,2,3,1,2,3,1,2,3)) #each {1,2,3} is a group
mod = lm(y~Treatment) #linear regression
print(anova(mod))
#解釋:Df(degree of freedom)
#Sum Sq: deviance (within groups, and residuals) 總偏差和
# Mean Sq: variance (within groups, and residuals) 平均方差和
# compare the contribution given by Treatment and Residual
#F value: Mean Sq(Treatment)/Mean Sq(Residuals)
#Pr(>F): p-value. 根據(jù)p-value決定是否接受Hypothesis H0:多個(gè)樣本總體均數(shù)相等(檢驗(yàn)水準(zhǔn)為0.05)
qqnorm(mod$residual) #plot the residual approximated by mod
#如果qqnorm of residual像一條直線,說明residual符合正態(tài)分布,也就是說Treatment帶來的contribution很小,也就是說Treatment無法帶來收益(多喂維他命少喂維他命沒區(qū)別)

如下面兩圖分別是 

(左)用 y = matrix(c(1,10,1,2,10,2,1,9,1),9,1)和

(右)y = rnorm(9);

的結(jié)果??梢娙绻o定豬吃維他命2后體重特別突出的數(shù)據(jù)結(jié)果后,qq圖種residual不在是一條直線,換句話說residual不再符合正態(tài)分布,i.e., 維他命對(duì)豬的體重有影響。


數(shù)據(jù)分析咨詢請(qǐng)掃描二維碼

若不方便掃碼,搜微信號(hào):CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對(duì)象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺(tái)檢測(cè)極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請(qǐng)參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請(qǐng)輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請(qǐng)輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }